简体   繁体   中英

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.

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. What I would like is to populate 'id' with the users details. The error message i'm getting at the moment is:

TypeError: Cannot read property 'paths' of undefined

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
     },
 }],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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