如何同时填充同一模型的两个字段
我正在尝试同时访问用户的关注者和帖子。
模型
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(...)