首页 > 解决方案 > 为什么在 typeorm 和 nestjs 中 InsertResult 为空

问题描述

当我尝试将一些数据插入存储库时。

const newChild = await queryRunner.manager.insert<Child>('child', child);
_height.childId = newChild.generatedMaps[0].id;

我遇到了以下错误。

error TypeError: Cannot read property 'id' of undefined

我检查了InsertResult

console.log(newChild);

InsertResult {
  identifiers: [],
  generatedMaps: [],
  raw: OkPacket {
    fieldCount: 0,
    affectedRows: 1,
    insertId: 7,
    serverStatus: 3,
    warningCount: 0,
    message: '',
    protocol41: true,
    changedRows: 0
  }
}

似乎identifiers是空的。

这可能是什么原因?

如果有人有意见,请告诉我。

谢谢

标签: typescriptnestjstypeorm

解决方案


对我来说,需要在 typeorm 配置中设置“实体”属性。

这样的事情解决了我的问题:

ormconfig.json

{
...
"username": "root",
"password": "admin",
"database": "test",
...

"entities": [
  "dist/**/entity/**/*.js"
],

推荐阅读