Nestjs application - Mongoose hanging in any query

Viewed 20

I have a NestJS application running with Node 16.17.0 and MongoDB 6.0.1. The MongoDB is running as a Docker container under Windows with WSL2 using Ubuntu 20.04 LTS. The Node was installed into Ubuntu using NVM.

The Nestjs application successfully starts and loads modules.

For some reason, when a query is executed, the application hangs. No error occurs. Try/catch can't get anything, no timeout, no error messages.

I tried to run the Node.js application as a Docker container, and the same error happened.

Any ideas?

Nestjs start log

[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [NestFactory] Starting Nest application...
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [InstanceLoader] AppModule dependencies initialized +36ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [InstanceLoader] MongooseModule dependencies initialized +1ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [InstanceLoader] PosterrModule dependencies initialized +0ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [InstanceLoader] MongooseCoreModule dependencies initialized +11ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [InstanceLoader] MongooseModule dependencies initialized +1ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [RoutesResolver] PostController {/test}: +2ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [RouterExplorer] Mapped {/test, GET} route +2ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [RouterExplorer] Mapped {/test/:id, GET} route +1ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [RouterExplorer] Mapped {/test, POST} route +1ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [RouterExplorer] Mapped {/test/:id, PUT} route +1ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [RouterExplorer] Mapped {/test/:id, DELETE} route +0ms
[Nest] 11168  - 09/24/2022, 4:50:57 PM     LOG [NestApplication] Nest application successfully started +2ms

docker-compose.json

version: '3.3'
services:
  MongoDB:
    image: 'mongo:latest'
    restart: 'unless-stopped'
    env_file: './.env'
    environment:
      - MONGO_DATABASE=test
    ports:
      - 27017:27017
    volumes:
      - mongodata:/data/db
volumes:
  mongodata: {}

MongoDB connection on app.module.ts:

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost:27017/test'),
    TeestModule,
  ],
})

Model loading on TestModule

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: 'Test', schema: testSchema},
    ]),
  ],
  controllers: [TestController]
})

I didn't use decorators for schema:

export const testSchema: Schema = new Schema(
  {
    id: { type: Types.ObjectId, required: false },
    text: string
  },
  { timestamps: true },
);

export interface TextDocumentInterface extends Document {
  id?: Types.ObjectId;
  text: string;
  createdAt: Date;
  updateddAt?: Date;
  deletedAt?: Date;
}
export default model<TextDocumentInterface >('Test', testSchema);

The model is injected on a repository class

Injectable();
export default class TestRepository {
  constructor(
    @InjectModel('Test') readonly model: Model<TextDocumentInterface>,
  ) {

  }
}

When the application calls a query, all freezes:

await this.model.find({}).limit(10);
0 Answers
Related