NestJS集成Mongoose启动失败:Cannot read 'tree'错误排查
问题描述
我正在学习NestJS,尝试使用Mongoose集成本地MongoDB,参考教程创建了coupon-service项目,启动时出现如下错误:
[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)
可能原因及解决方法
1. Schema定义或注册错误
- 问题:Schema类未用
@Schema()装饰器正确修饰,字段定义不符合Mongoose规范,或者Module中未正确注册Schema,导致Model实例化时找不到合法的Schema对象。 - 解决:
- 检查Schema文件,确保结构符合规范:
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; @Schema() export class Coupon extends Document { @Prop({ required: true }) code: string; @Prop({ required: true }) discount: number; } export const CouponSchema = SchemaFactory.createForClass(Coupon); - 在对应Module中注册Schema:
import { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; import { Coupon, CouponSchema } from './schemas/coupon.schema'; import { CouponService } from './coupon.service'; @Module({ imports: [MongooseModule.forFeature([{ name: Coupon.name, schema: CouponSchema }])], providers: [CouponService], }) export class CouponModule {}
- 检查Schema文件,确保结构符合规范:
2. NestJS与Mongoose版本不兼容
- 问题:
@nestjs/mongoose和mongoose版本不匹配,导致内部调用出现属性未定义的异常。 - 解决:
- 查看
package.json中的版本,参考NestJS官方文档确认兼容版本(比如@nestjs/mongoose@10.x对应mongoose@7.x+)。 - 卸载现有依赖后重新安装兼容版本:
npm uninstall @nestjs/mongoose mongoose npm install @nestjs/mongoose@适配版本 mongoose@对应兼容版本
- 查看
3. Model注入方式错误
- 问题:在Service中直接注入Model类,未使用
@InjectModel()装饰器,导致Nest无法正确实例化Mongoose Model。 - 解决:
- 按规范在Service中注入Model:
import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Coupon } from './schemas/coupon.schema'; @Injectable() export class CouponService { constructor(@InjectModel(Coupon.name) private couponModel: Model<Coupon>) {} }
- 按规范在Service中注入Model:
4. Schema文件导出错误
- 问题:Schema文件未正确导出Schema对象或对应的类,导致Module注册时无法获取到有效Schema。
- 解决:检查Schema文件的导出语句,确保
Coupon类和CouponSchema都通过export关键字正确导出,避免默认导出与命名导出混用导致的导入错误。
内容的提问来源于stack exchange,提问作者Đức Trường Đặng
相关产品推荐
相关产品推荐

