【发布时间】:2021-12-01 13:23:21
【问题描述】:
我正在尝试通过环境文件将连接参数传递给 mongodb,但出现以下错误
[Nest] 1176 - 12/10/2021 23:34:35 ERROR [ExceptionHandler] Cannot read property 'get' of undefined
TypeError: Cannot read property 'get' of undefined
at MongoService.createMongooseOptions (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/src/common/mongo/mongo.service.ts:20:41)
at Function.<anonymous> (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:135:120)
at Generator.next (<anonymous>)
at /Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:16:12)
at InstanceWrapper.useFactory [as metatype] (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:135:45)
at Injector.instantiateClass (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/core/injector/injector.js:294:55)
at callback (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/core/injector/injector.js:43:41)
at Injector.resolveConstructorParams (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/core/injector/injector.js:119:24)
这是服务 mongo.service.ts
import { MongooseModuleOptions, MongooseOptionsFactory } from '@nestjs/mongoose';
import { Configuration } from '../../config/config.keys';
import { ConfigService } from '../../config/config.service';
export class MongoService implements MongooseOptionsFactory {
constructor( private configService: ConfigService ) {}
createMongooseOptions(): MongooseModuleOptions {
const user = this.configService.get(Configuration.DB_MONGO_USER);
const password = this.configService.get(Configuration.DB_MONGO_PASSWORD);
const server = this.configService.get(Configuration.DB_MONGO_HOST);
const database = this.configService.get(Configuration.DB_MONGO_DATABASE);
return {
uri: `mongodb://${user}:${password}@${server}/${database}?retryWrites=true&w=majority`,
};
}
}
我将它导入到 app.module.ts 中
@Module({
imports: [
MongooseModule.forRootAsync({
useClass: MongoService,
}),
任何建议, 谢谢, 杰米
【问题讨论】:
-
您是否在此站点中搜索过无法获取未定义的属性?从字面上看,之前已经有数百个(如果不是数千个)与之前在此处提出(并回答)的相同错误相关的问题。他们中的一个肯定可以为您指出解决此问题的方法。
-
可能你忘了在
MongoService上面加上@Injectable() -
添加@Injectable 我收到以下错误:错误:Nest 无法解析 MongoService (?) 的依赖项。请确保索引 [0] 处的参数 ConfigService 在 MongooseCoreModule 上下文中可用。
标签: nestjs