给定一个简单的 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/