gpt4 book ai didi

mongoose - 无法在 NestJS 应用程序中启动和运行 Mongoose 虚拟

转载 作者:行者123 更新时间:2023-12-05 06:05:32 24 4
gpt4 key购买 nike

我目前正在从事一个非常需要虚拟填充的项目:

有一个客户方案、一个文档方案以及其他一些引用客户的方案。我想在客户内部填充引用的项目。

我已经有一个使用 Typegoose 的工作模型,但由于后期不兼容,我不得不将其从项目中删除。这是它之前的工作方式:

  @prop({ 
ref: () => DMSDocument,
foreignField: 'linkedCustomers', // compare this value to the local document populate is called on
localField: '_id', // compare this to the foreign document's value defined in "foreignField",
justOne: false
})
public documents: { type: Types.ObjectId, ref: 'DMSDocument' }[];

现在我试图在删除 typegoose 之后使用 nestjs/mongoose 实现此目的:

@Prop({ 
virtual: 'documents',
ref: 'DMSDocument',
foreignField: 'linkedCustomers',
localField: '_id',
})
public documents: DMSDocument[];

虚拟 setter/getter 工作得很好,因为我只是在使用

@Schema({ toJSON: { virtuals: true, getters: true }, toObject: { virtuals: true, getters: true }})

模型是这样填充的:

this.customerModel.findOne(params)
.populate('documents', '', 'DMSDocument')
.populate('repairs', '', 'Repair')

我不知何故卡住了 - 我只是得到空数组,没有错误。我错过了什么?他们甚至可以使用 nestjs/mongoose 吗?

最佳答案

好吧,我设法让事情重新开始,但不是以我喜欢的方式:

定义类并通过以下方式将其转换为架构后

export const CustomerSchema = SchemaFactory.createForClass(Customer);

我只需要“手动”添加虚拟填充:

CustomerSchema.virtual('documents', {
ref: 'DMSDocument',
localField: '_id',
foreignField: 'links.customers',
justOne: false
});

一切都按预期工作,但我更喜欢使用装饰器的方式。

关于mongoose - 无法在 NestJS 应用程序中启动和运行 Mongoose 虚拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65995610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com