Javascript Javascript删除对象属性不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33239464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript delete object property not working
提问by ketysek
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:
我正在 MEAN.js 上运行一些项目,但遇到了以下问题。我想进行一些用户的个人资料计算并将其保存到数据库中。但是用户模型中的方法存在问题:
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 :)
怎么了?为什么 delete 方法返回 true,但没有任何反应?谢谢你的帮助 :)
采纳答案by Virendra yadav
there are certain rules for delete operator in javascript
javascript中删除操作符有一定的规则
- if the property is an own non-configurable property in "strict mode" than it will return false.
- 如果该属性在“严格模式”下是自己的不可配置属性,则它将返回 false。
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
- If the object inherits a property from a prototype, and doesn't have the property itself, the property can't be deleted by referencing the object. You can, however, delete it directly on the prototype.
- 如果对象从原型继承了一个属性,并且没有该属性本身,则无法通过引用该对象来删除该属性。但是,您可以直接在原型上删除它。
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
所以,请交叉检查这些点,有关更多信息,您可以阅读此链接
回答by LEMUEL ADANE
Just do:
做就是了:
user.password = undefined;
instead of:
代替:
delete user.password;
and the password property will not appear at the output.
并且密码属性不会出现在输出中。
回答by Ruslan Suvorov
Had a similar issue. The delete
operator "was not working" when trying to delete a property from an object in a specific case. Fixed it using Lodash
unset
:
有一个类似的问题。delete
在特定情况下尝试从对象中删除属性时,运算符“不起作用”。修复它使用Lodash
unset
:
_.unset(user, "password");
_.unset(user, "password");
https://lodash.com/docs/4.17.11#unset
https://lodash.com/docs/4.17.11#unset
Otherwise the delete
operator does work. Just in case, delete
operator docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
否则,delete
操作员确实工作。以防万一,delete
操作员文档在这里:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete