How do I populate two fields of the same model at the same time

I am trying to access users' followers and posts at the same time.

The model

const userSchema = new mongoose.Schema({
    username:String,
    password:String,
    firstName:String,
    lastName:String,
    email:String,
    profileImage:String,
    isAdmin:{type:Boolean, default:false},
    posts:[
        {
            type:mongoose.Schema.Types.ObjectId,
            ref:'Campground'
        }
    ],
    followers:[
        {
            type:mongoose.Schema.Types.ObjectId,
            ref:'User'
        }
    ],
    notifications:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Notification'
    }]
});  

        User.findById(req.user._id).populate('posts').populate('followers').exec((err,user)=>{                    
     for(let follower of user.followers){...}

I am getting the error - "Cannot read property 'followers' of undefined"

I have tried both

.poulate('posts followers')

and

.populate('posts')
.populate('followers')
.exec(...)

0 Answers