I have defined Getters in UserSchema, getters are getting called when querying the User Model, But when querying through the populate method the getters are not being called. How can we invoke getters in such an instance? Please find below the query and schema.
Query:
Post.findById(req.query.postId, '-updated_at -created_at')
.populate('_user userInvited._user','_id username photo')
.exec(function (err, post) { // Handle Actions });
User Schema
// 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;
}
Post Schema
// 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'}});