node.js - 需要帮助来确定问题
问题描述
以下是我的 main.ts 片段
try {
process.on('unhandledRejection', (err) => {
// tslint:disable-next-line:no-console
console.error(err); <------- no console
});
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
app.enableCors();
app.useGlobalFilters(new AllExceptionsFilter());
await app.listen(3000, () => {
// tslint:disable-next-line:no-console
console.log('listen callback'); <------- no console
}).then(() => {
// tslint:disable-next-line:no-console
console.log('started'); <------- no console
});
} catch (error) {
// tslint:disable-next-line:no-console
console.log('catch', error); <----- only this gets consoled as undefined
}
我的应用程序连接到数据库(已验证),只是它没有启动。
解决方案
使用回调参数调用.listen()
意味着此方法将返回undefined
。您正在尝试调用.then()
该undefined
值。
您只需要将您的收听电话更改为:
await app.listen(3000, () => 'Server running on port 3000...');
奖励:您可以通过将调用设置listen
为变量然后控制台记录它来自己测试:
const result = await app.listen(3000, () => 'Server running on port 3000...');
console.log(result);
//undefined
推荐阅读
- python - 在pygame中用wasd和箭头键分别移动两个矩形?
- c++ - 完美转发的pair的返回元素
- javascript - 时显示错误信息
- communication - 什么时候用 LdCom 代替 COM?
- python - 多个阵列的两点之间的最小距离
- firebase - Firebase 模拟器对 Google Cloud Speech to Text Api 的请求被拒绝
- c# - 您无法添加或更改记录,因为表 'tbl_routines' 中需要相关记录
- javascript - Material UI Checkbox 在一个实例中重复相同的值两次
- c# - 启动应用程序时发生错误。Microsoft.AspNetCore.Hosting.Server.IServer
- c# - 如何使用 FluentDocker 连接到 mongodb