I'm running some project on MEAN.js and I've got a following problem. I want to make some user's profile calculation and the save it to database. But there's a problem with method in users model:
UserSchema.pre('save', function(next) { if (this.password && this.password.length > 6) { this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64'); this.password = this.hashPassword(this.password); } next(); });
If I will send a password with my changes, it will change credentials, so user is unable to login next time. I want to delete password from user object before save, but I'm not able to do it (let's look at the comments in my code below):
exports.signin = function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err || !user) { res.status(400).send(info); } else { /* Some calculations and user's object changes */ req.login(user, function(err) { if(err) { res.status(400).send(err); } else { console.log(delete user.password); // returns true console.log(user.password); // still returns password :( //user.save(); //res.json(user); } }); } })(req, res, next); };
What's wrong? Why the delete method returns true, but nothing happens? Thanks for your help :)
You can only delete a Property or Method of an object using the Delete Operator. It will completely remove the Property from the collection. It also does not remove the Non-configurable properties. Delete returns false if the property is an own property and cannot be deleted.
The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
The semantically correct way to remove a property from an object is to use the delete keyword.
The only way to fully remove the properties of an object in JavaScript is by using delete operator. If the property which you're trying to delete doesn't exist, delete won't have any effect and can return true.
Just do:
user.password = undefined;
instead of:
delete user.password;
and the password property will not appear at the output.
there are certain rules for delete operator in javascript
for example
x = 42; // creates the property x on the global object var y = 43; // creates the property y on the global object, and marks it as non-configurable // x is a property of the global object and can be deleted delete x; // returns true // y is not configurable, so it cannot be deleted delete y; // returns false
for example
function Foo(){} Foo.prototype.bar = 42; var foo = new Foo(); // returns true, but with no effect, // since bar is an inherited property delete foo.bar; // logs 42, property still inherited console.log(foo.bar);
so, please cross check these point and for more information your can read this Link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With