node.js - `repository.create` 去除既是数组又是嵌入对象的列的值

标签 node.js mongodb typescript nestjs typeorm

给定一个简单的 Foo 实体,该实体又包含 mongodb 中的 Bar 对象的集合

仅当列既是数组又是嵌入对象时才会出现问题。

@Entity()
export class Foo {
  @ObjectIdColumn()
  public id: ObjectID;

  @Column()
  public simple: string;

  @Column(type => Bar)
  public collection: Bar[];
}

export class Bar {
  @Column()
  value: boolean;
}

repository.create 转换原始值

{
  "simple": "string",
  "collection": [
    { "value": true },
    { "value": false }
  ]
}

简单地

{ "simple": "string" }

我刚刚从 https://github.com/typeorm/typeorm/issues/2342 获取此信息但同样的事情也发生在我身上

最佳答案

显然,这是一个 bug在打字机中。作为解决方法,您可以手动设置集合,直到问题得到解决:

async createFoo(createFooDto) {
  const newFoo = await this.repository.create(createFooDto);
  // TODO: Remove when https://github.com/typeorm/typeorm/issues/1980 is solved
  newFoo.collection = createFooDto.collection;
  this.repository.save(newFoo);
}

如果这是一个回归(它曾经有效),您可以尝试降级 typeorm 直到它被修复。

关于node.js - `repository.create` 去除既是数组又是嵌入对象的列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54815740/

相关文章:

node.js - 如何通过电子邮件而不是 passport.js 中的用户名进行身份验证?

node.js - 设置 GrapqQL 日期格式

java - MongoDB 查询匹配单个条目和数组元素

javascript - 使用 Typescript 捕获 Angular 中的点击事件?

visual-studio-2012 - 来自 Typescript jsdoc 的 Visual Studio intellisense 不适用于胖箭头功能

javascript - Node.js + CoffeeScript - 模块/类混淆

javascript - 在 Angular 5 中设置 header

javascript - 如何在 vanilla Javascript 文件中导入 nodejs 模块

javascript - 在 Array Node JS 中获取多个对象中的特定键

node.js - 类型 'virtual' 上不存在属性 'typeof Schema'