首页 > 解决方案 > 切换到 esnext 后,Typescript express 无法正常工作

问题描述

我正在开发一个反应打字稿应用程序。由于“顶级等待”问题,我想将我的目标和模块分别更改为“es2017”和“esnext”。将目标更改为“es2017”工作正常,但是当我将模块更改为“esnext”时,我的应用程序在启动时崩溃。

问题是不再有效的快速路线:

Route.get() requires a callback function but got a [object Undefined]

这是我的 server.ts 中的特定代码

const {all_users} = require("./controllers/UserController");
express.get("/users", all_users);

用户控制器.ts:

import "reflect-metadata";
import {createConnection} from "typeorm";
import {User} from "../entities/User";


exports.all_users = function(req, res) {
    createConnection().then(async connection => {
        const users = await connection.manager.find(User);        
        res.json({users});
    }).catch(error => console.log(error));
};

tsconfig.json:

{
    "compilerOptions": {
        "lib": [
            "es5",
            "es6"
        ],
        "target": "es2017",
        "module": "esnext",
        "moduleResolution": "node",
        "outDir": "./build",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,      
        "noImplicitAny": false,
        "strictNullChecks": true,
        "jsx": "react",
        "resolveJsonModule": true      
    },   
    "include": [
        "src/**/*.ts"         
    ]
}

这是 esnext 的一个已知问题吗?

标签: reactjstypescriptexpresstypeorm

解决方案


我不得不将声明更改all_users为:

export function all_users(req, res) {

推荐阅读