0

我正在尝试填充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 },
});

当我尝试.profilejob.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用户对象的虚拟属性。

4

2 回答 2

0

我不知道这是否是可取的,但我得到它与以下工作:

_.each(jobs, function(job, i){
    if ( job.creator ) {
        job._doc.creator = job.creator.profile;
    }
});

res.json(jobs);
于 2014-07-15T03:14:10.277 回答
0

我建议不要这样做job.creator = job.creator.profile,这与 Mongoose 的工作方式不太相符。还有,从哪里来profileImageUrls?似乎不在架构中。

于 2014-07-14T15:01:55.297 回答