填充相同模型的“兄弟姐妹”

时间:2019-05-16 14:34:42

标签: mongodb mongoose-populate

如何同时填充同一模型的两个字段

我正在尝试同时访问用户的关注者和帖子。

模型

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){...}

我遇到错误-“无法读取未定义的属性'followers'”

我都尝试过

.poulate('posts followers')

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

0 个答案:

没有答案