My problem is that I want to update a user password that's already stored on MongoDB, and I can't call a method for User. Let me show the code:
User schema:
userSchema = new Schema({ ... });
userSchema.methods.setPassword = function (passwordPlainText) {
this.passwordHash = createHash(passwordPlainText, this.salt);
};
module.exports = mongoose.model('User', userSchema);
And it works fine if I made this:
user = new User();
user.setPassword('foobar');
But if I want to do something like this:
User.findOne({email: req.param('email')}, function (err, user) {
user.setPassword('foobar');
});
It outputs:
TypeError: Object #<Object> has no method 'setPassword'
Can someone please help me to find a way to call these schema methods after retrieving the user from database?
Additional info:
Found the problem, seems that the user I was saving in the session didn't persists the schema methods for user. Solved the problem saving just the userId in the session, calling another User.findOne(id) and then calling setPassword method.