首页 > 解决方案 > 需要帮助来确定问题

问题描述

以下是我的 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
  }

我的应用程序连接到数据库(已验证),只是它没有启动。

标签: node.jsnestjsfastify

解决方案


使用回调参数调用.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

推荐阅读