【问题标题】:`repository.create` strips values of columns that are both arrays and embedded objects`repository.create` 去除既是数组又是嵌入对象的列的值
【发布时间】:2019-07-15 21:04:42
【问题描述】:

给定一个简单的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 拿了这个,但同样的事情发生在我身上

【问题讨论】:

  • 还有问题吗?

标签: node.js mongodb typescript nestjs typeorm


【解决方案1】:

显然,这是 typeorm 中的 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 直到它被修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多