reactjs - 切换到 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 的一个已知问题吗?
解决方案
我不得不将声明更改all_users
为:
export function all_users(req, res) {
推荐阅读
- bots - 如何为 Discord 机器人运行此 .js 代码?
- python - Keras 减重
- prolog - 序言 | 我可以在假后继续执行吗?
- php - 在 Woocommerce 结帐页面中将产品添加到购物车的复选框字段
- python - 为什么 ByteArray() 组合数组值?(Python)
- java - 使用 Hibernate、Spring Boot 和 JavaFX 生成 JAR
- node.js - 如何在 NEXT.js 中创建子页面
- angular - Angular 7 Component Observable Named vs Anonymous 函数
- scala - 从 RequestContext 中访问 akka-http 中的路径段?
- search - kentico 中的页面爬虫索引未显示结果中的内容