Node.js Mongoose填充未提供连接结果
我有两种型号,叫做TodaysDels和ProductsNode.js Mongoose填充未提供连接结果,node.js,mongodb,mongoose,typegoose,Node.js,Mongodb,Mongoose,Typegoose,我有两种型号,叫做TodaysDels和Products export class TodaysDeal { _id: ObjectId; @Property({ required: true, type: Schema.Types.ObjectId, ref: "ProductsModel" }) products: Products } export const TodaysDealModel = getModelForClass(Todays
export class TodaysDeal {
_id: ObjectId;
@Property({ required: true, type: Schema.Types.ObjectId, ref: "ProductsModel" })
products: Products
}
export const TodaysDealModel = getModelForClass(TodaysDeal);
及
我正在尝试填充联接数据,但未获得联接结果。它只有contain product.\u id
这是我的密码
let data = await TodaysDealModel.find().populate("ProductsModel");
您应该为
populate
方法提供todaysdelmodel
model中提供的字段名
试一试
let data=wait TodaysDealModel.find().populate(“产品”);
扩展@Vishnu所说的:你有2.5个问题
populate
,您需要使用字段名而不是引用的模型名ProductsModel
,至少不是您的代码示例提供的型号名称产品
作为类型,其中Ref
是正确的
更正后的代码如下所示:
export class TodaysDeal {
_id: ObjectId;
@Property({ required: true, type: Schema.Types.ObjectId, ref: "Products" })
products: Ref<Products>;
}
export const TodaysDealModel = getModelForClass(TodaysDeal);
export class Products {
_id: ObjectId;
@Property({ required: true })
productName: String;
}
export const ProductsModel = getModelForClass(Products);
export class TodaysDeal {
_id: ObjectId;
@Property({ required: true, type: Schema.Types.ObjectId, ref: "Products" })
products: Ref<Products>;
}
export const TodaysDealModel = getModelForClass(TodaysDeal);
export class Products {
_id: ObjectId;
@Property({ required: true })
productName: String;
}
export const ProductsModel = getModelForClass(Products);
let data = await TodaysDealModel.find().populate("products").exec();