需要帮助来确定问题

时间:2019-12-18 08:29:38

标签: node.js nestjs fastify

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

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

1 个答案:

答案 0 :(得分:1)

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