2014-07-14 2 views
0

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이 없습니다.

답변

0

job.creator = job.creator.profile 라인을 사용하지 않는 것이 좋습니다. 몽구스의 작동 방식과 잘 맞지 않습니다. 또한, profileImageUrls은 어디에서 오는가? 스키마에없는 것 같습니다. 이 바람직한 경우

+0

? 나는 그것을 작동 시키지만'job._doc.creator = job.creator.profile'을 할 때 어떤 영향이 있는지 모른다. – chovy

0

는 잘 모르겠지만, 나는 다음과 같은 작업있어 :

내 게시 대답을 볼 수
_.each(jobs, function(job, i){ 
    if (job.creator) { 
     job._doc.creator = job.creator.profile; 
    } 
}); 

res.json(jobs);