【问题标题】:Cannot read property 'prototype' of undefined - nestjs with typeorm and mongoDB无法读取未定义的属性“原型”-带有 typeorm 和 mongoDB 的 nestjs
【发布时间】:2021-11-20 13:36:47
【问题描述】:

我正在使用带有 typeorm 和 mongodb 的 nestjs 设置一个简单的 API。但是,当我发出一个简单的 http 请求时,会出现以下错误

[Nest] 8852 - 09/28/2021, 6:06:03 PM [ExceptionsHandler] Cannot read property 'prototype' of undefined +4874934ms TypeError: Cannot read property 'prototype' of undefined at FindCursor.cursor.toArray (C:\Users\admin\Documents\projects\shoplist\api\node_modules\typeorm\entity-manager\MongoEntityManager.js:669:37) at MongoEntityManager.<anonymous> (C:\Users\admin\Documents\projects\shoplist\api\node_modules\typeorm\entity-manager\MongoEntityManager.js:60:54) at step (C:\Users\admin\Documents\projects\shoplist\api\node_modules\tslib\tslib.js:143:27) at Object.next (C:\Users\admin\Documents\projects\shoplist\api\node_modules\tslib\tslib.js:124:57) at fulfilled (C:\Users\admin\Documents\projects\shoplist\api\node_modules\tslib\tslib.js:114:62) at processTicksAndRejections (internal/process/task_queues.js:95:5)

offices.controller.ts

@Get('/list')
findAll() {
  return this.officesService.findAll();
}

offices.service.ts

async findAll() {
  try {
    const offices = await this.officesRepository.find();

    return { success: true, message: 'Office successfully retrieved!', data: offices }
  } catch(e) {
    throw new InternalServerErrorException({ success: false, error: e.message })
  }
}

老实说,我不知道为什么它会给我错误 500 的消息。知道发生了什么吗?

【问题讨论】:

  • 你分享的sn-ps没问题。其中哪一行触发了该错误?
  • 我不确定。我已更新问题以包含完整的堆栈跟踪
  • 您使用的是 Mongo v4 吗?如果是这样,TypeORM 还不支持它
  • 我使用的是 mongo v4.4.6。这很奇怪,因为一些 http 调用正在工作,我可以看到存储在我的数据库中的值

标签: mongodb nestjs typeorm


【解决方案1】:

mongodb版本的问题typeorm

typeorm 尚不支持 >3 版本。

运行这个来解决问题。

mongodb@3 @types/mongodb@3

完整的依赖关系

npm install typeorm @nestjs/typeorm mongodb@3 @types/mongodb@3

在此之后解决了相同的问题。

【讨论】:

    【解决方案2】:

    这些错误看起来像是 mongodb 版本问题。如果你使用的是 mongodb > 3,Typeorm 还不支持。他们在 github 上打开了 PR --> https://github.com/typeorm/typeorm/issues/7907

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2021-10-24
      • 2021-02-27
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2021-11-16
      相关资源
      最近更新 更多