简体   繁体   English

Mongo DB-深度填充无法正常工作

[英]Mongo DB - Deep populate not working

I have a project that uses Node & Mongoose/ Mongo DB and i'm having trouble doing a deep populate on one of my models. 我有一个使用Node&Mongoose / Mongo DB的项目,但我无法对其中一个模型进行深入研究。

I found this plugin to manage my deep population. 我找到了这个插件来管理我的深层人口。 Here the setup in my controller: 这是我的控制器中的设置:

const board = await Board.find({'_id':boardId}).deepPopulate('members.id')

Here is an image of my stored DB board value in compass: 这是我在指南针中存储的数据库板值的图像:

在此处输入图片说明

The id's in the property 'id' in the members array are that of user ID's. 成员数组的属性“ id”中的ID是用户ID的ID。 What I would like is to populate 'id' with the users details. 我想用用户详细信息填充“ id”。 The error message i'm getting at the moment is: 我现在收到的错误消息是:

TypeError: Cannot read property 'paths' of undefined TypeError:无法读取未定义的属性“路径”

Here is my board schema: 这是我的电路板架构:

const boardSchema = new Schema({
    name:{
        type:String,
        trim:true,
        required:'Please enter a board name'
    },
    owner:{
        type:mongoose.Schema.ObjectId,
        ref:'User'
  },
    members:{
    type:Array,
  }     
});

boardSchema.plugin(deepPopulate);

Not entirely sure why it's not populating here? 不能完全确定为什么它不在这里吗?

You need to define the schema for the members array too, only then mongoose will know how to populate it. 您还需要为成员数组定义架构,只有猫鼬才会知道如何填充它。

 members: [{
     privilege: String,
     id: {
         type:mongoose.Schema.ObjectId,
         ref:'User' // assuming its an user
     },
 }],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM