オブジェクト ( )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
User オブジェクトの仮想属性であるそれも欠落しています。