Wednesday, 15 August 2012

node.js - How to change the document using pre save in Mongoose -



node.js - How to change the document using pre save in Mongoose -

i trying assign pre handler mongoose save event , encrypt document before saving:

usershecma.pre('save', function(next) { var self = {}; self.key = this.password;; self.encriptedstring = encrypt.encrypt(json.stringify(this), this.password); self.user = this.user self.decrypt = function() { var user = json.parse(encrypt.decrypt(this.encriptedstring, this.key)); for(var key in user) { this[key] = user[key]; } } for(var key in this){ delete this[key]; } for(var key in self){ this[key] = self[key]; } console.log(this); next(self); });

i have tried bunch of diffrent things, error, doesnt alter document.

let me know if need more info, ari

edit: tried benoir's answer, can't edit this.

i believe calling next(self) create next handler think there error , not save document.

you should phone call next()

look @ http://mongoosejs.com/docs/middleware.html under 'error handling'

node.js mongodb mongoose save

No comments:

Post a Comment