Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose填充未提供连接结果_Node.js_Mongodb_Mongoose_Typegoose - Fatal编程技术网

Node.js Mongoose填充未提供连接结果

Node.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

我有两种型号,叫做TodaysDels和Products

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();