我在UserSchema中定义了Getters,在查询用户模型时调用了getter,但是当通过populate方法查询时,没有调用getter。我们如何在这样的实例中调用getter?请在下面找到查询和架构。
查询:
Post.findById(req.query.postId, '-updated_at -created_at')
.populate('_user userInvited._user','_id username photo')
.exec(function (err, post) { // Handle Actions });
用户架构
// user schema
var UserSchema = new Schema({
name: String,
username: String,
photo: {type: String, get: imagePath, default: ''},
}, {timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}});
// Getters
function imagePath(photo) {
var image = (photo && photo != "") ? photo : App.imageUrl('user.jpg');
return image;
}
发布架构
// Define User Schema
var userSchema = [{type: Schema.Types.ObjectId,ref: 'User'}];
// Define Post Schema
var PostSchema = new Schema({
_user: {type: Schema.Types.ObjectId,ref: 'User',required: true},
text: {
type: String,
required: true
},
userInvited: userSchema,
}, {timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}});