我正在尝试填充job.creator
并使用User
对象 ( user.profile
) 中的虚拟属性来仅为作业创建者提供公共信息。
/**
* User Schema
*/
var UserSchema = new Schema({
favorites: {
jobs: [{ type: Schema.ObjectId, ref: 'Job', index: true }]
}
});
// Public profile information
UserSchema
.virtual('profile')
.get(function () {
return {
favorites: this.favorites,
profileImageUrls: this.profileImageUrls //also not being populated
};
});
UserSchema
.virtual('profileImageUrls')
.get(function(){
var defaultUrl = cfg.home + '/images/cache/default-profile-img.png'
, smallUrl = cfg.home + '/images/cache/default-profile-img-small.png';
return {
small: smallUrl
, default: defaultUrl
};
});
/**
* Job Schema
*/
var JobSchema = new Schema({
creator: { type: Schema.ObjectId, ref: 'User', index: true, required: true },
});
当我尝试.profile
从job.creator
对象中获取虚拟属性时,我缺少一些值:
//controller
Job.populate(job, { path: 'creator', select: '-salt -hashedPassword' }, function(err, job){
if ( job.creator ) {
job.creator = job.creator.profile; //this is missing some attributes
}
return res.json(job);
})
{
//...
creator: {
favorites: {
jobs: [ ] //this is not being populated
}
}
}
它还缺少job.creator.profileImageUrls
用户对象的虚拟属性。