0

Following is my snippet of 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
  }

My app gets connected to database (verified), just that it doesn't start.

Mubasshir Pawle
  • 309
  • 1
  • 3
  • 19

1 Answers1

1

Calling .listen() with a callback argument means this method is going to return undefined. You are trying to call .then() on that undefined value.

You need simply need to change your listen call to this:

await app.listen(3000, () => 'Server running on port 3000...');

BONUS: You can test this yourself by setting that call to listen to a variable and then console log it:

const result = await app.listen(3000, () => 'Server running on port 3000...');

console.log(result);
//undefined
nerdy beast
  • 779
  • 5
  • 8
  • Thanks for revert, I understand what you are saying, I've tried above but still, the server doesn't start. Instead, I'm getting unHandledRejection error, that's the reason I've wrapped around try-catch. – Mubasshir Pawle Dec 19 '19 at 04:35
  • Do you have this code in Github or somewhere where we can take a look? – nerdy beast Dec 19 '19 at 17:14