`repository.create`剥离同时包含数组和嵌入对象的列的值

问题描述 投票:1回答:1

给出一个简单的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个回答
1
投票

显然,这是一种类型的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直到它被修复。

© www.soinside.com 2019 - 2024. All rights reserved.