Search code examples
mongoosenestjs

Failed to start NestJS application with Mongoose


I'm learning NestJs and trying to integrate with MongoDB (local) using Mongoose.

By getting the idea from here: https://medium.com/globant/crud-application-using-nestjs-and-mongodb-99a0756adb76

I've made my own application called coupon-service: https://github.com/ductruong253/coupon-service/tree/feature/db-config

But when I tried to start the application, there was an error:

[Nest] 21089  - 05/05/2023, 12:25:36 AM     LOG [NestFactory] Starting Nest application...
[Nest] 21089  - 05/05/2023, 12:25:37 AM   ERROR [ExceptionHandler] Cannot read properties of undefined (reading 'tree')
TypeError: Cannot read properties of undefined (reading 'tree')
    at Model.Document.$__setSchema (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/mongoose/lib/document.js:3452:18)
    at Model.Document (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/mongoose/lib/document.js:88:10)
    at new Model (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/mongoose/lib/model.js:121:12)
    at Injector.instantiateClass (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/@nestjs/core/injector/injector.js:351:19)
    at callback (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/@nestjs/core/injector/injector.js:56:45)
    at Injector.resolveConstructorParams (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/@nestjs/core/injector/injector.js:136:24)
    at Injector.loadInstance (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/@nestjs/core/injector/injector.js:61:13)
    at Injector.loadProvider (/media/truongdd/ubuntu/learning/git/coupon-service/node_modules/@nestjs/core/injector/injector.js:88:9)
    at /media/truongdd/ubuntu/learning/git/coupon-service/node_modules/@nestjs/core/injector/instance-loader.js:56:13
    at async Promise.all (index 4)

What is the possible cause of this error and how to fix it?

Thanks,

Dan


Solution

  • I reviewed your code, this is why you can't start the app

      providers: [CouponInfoService, M̶o̶d̶e̶l̶],
    

    you don't need to add Model into providers, this is imported from mongoose package, you just

    import { Model } from 'mongoose'; and use it directly