I have a UserSchema in mongoose:
var UserSchema = new Schema({ name: String, username: String, email: String, role: {type: String, default: 'user'}, following: [{type: Schema.ObjectId, ref: 'User'}], followers: [{type: Schema.ObjectId, ref: 'User'}], hashedPassword: String, provider: String, salt: String, facebook: {}, twitter: {}, github: {}, google: {} });
I created a virtual profile that only returns information for a public profile:
UserSchema .virtual('profile') .get(function() { return { 'username': this.username, 'name': this.name, 'role': this.role }; });
My problem is how to get only this information when I make a find query. Here is my code:
UserSchema.statics = { list: function (options, callback) { var criteria = options.criteria || {}; this.find(criteria) .select() .limit(options.perPage) .skip(options.perPage * options.page) .exec(callback); } };
Of course, I can just put username , name and role , but in this case I will have the code repeat. Can i avoid this?
source share