【问题标题】:NestJS : TypeError: Cannot read property 'get' of undefinedNestJS:TypeError:无法读取未定义的属性“get”
【发布时间】: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


【解决方案1】:

你需要两件事:

  1. 您的MongoService 需要用@Injectable() 标记,以便Nest 可以读取构造函数的元数据并正确设置注入

  2. 如果您的ConfigModule 没有全局导出的ConfigService,那么在MongooseModule.forRootAsync()useClass 中,您需要拥有imports: [ConfigModule],以便您可以注入ConfigService

【讨论】:

  • 谢谢杰,我显然连接错了,我看不到它。使用 @Injectable() ,我收到以下错误:错误 [ExceptionHandler] Nest 无法解析 MongoService (?) 的依赖项。请确保索引 [0] 处的参数 ConfigService 在 MongooseCoreModule 上下文中可用。
  • 您是否将ConfigModule 添加到MongooseModule.forRootAsync() 选项中,就像我在第2 点中提到的那样?
  • 同样的错误:@Module({ imports: [ MongooseModule.forRootAsync({ imports: [ ConfigService ], useClass: MongoService, inject: [ConfigService], }), VentaNegociosModule, ConfigModule ],跨度>
  • 你有imports: [ConfigService],而不是imports: [ConfigModule]。你也不需要inject
猜你喜欢
  • 2017-04-28
  • 2020-09-04
  • 2021-04-29
  • 1970-01-01
  • 2021-12-31
  • 2021-12-17
  • 1970-01-01
  • 2020-07-14
  • 2020-04-18
相关资源
最近更新 更多