job.creator
을 채우고 User
객체의 가상 속성 (user.profile
)을 사용하여 작업 작성자에 대한 공개 정보 만 제공하려고합니다. populate()가 사용되는 경우에 mongoose 가상 속성이 채워지지 않음
/**
* 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 },
});
나는 job.creator
개체에서 가상 속성 .profile
을 얻으려고
//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
이 없습니다.
? 나는 그것을 작동 시키지만'job._doc.creator = job.creator.profile'을 할 때 어떤 영향이 있는지 모른다. – chovy