最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Javascript delete object.property not working - Stack Overflow

programmeradmin4浏览0评论

I am trying to delete an object's property but the property does not get deleted at all...

What I have:

var tagFound = yield tags.findById(this.params.tagId);
debug('prior delete: %j', tagFound);
delete tagFound.password;
debug('after delete: %j', tagFound);

What I get:

api_v1 prior delete: {"_id":"55e064e9727b44c32a262c0f","expires":"2015-08-29T13:40:57.673Z","password":"$2a$08$hucJyHIU5gholAB1L.wVKeFoTmvFho9xFiJAAvmwmtKphLuJ9Hq4K","type":"free","name":"teste","__v":0,"visible":true,"locations":[{"latitude":65.9667,"longitude":-18.5333,"_id":"55e064e9727b44c32a262c10","timestamp":"2015-08-28T13:40:57.572Z"}]}

api_v1 after delete: {"_id":"55e064e9727b44c32a262c0f","expires":"2015-08-29T13:40:57.673Z","password":"$2a$08$hucJyHIU5gholAB1L.wVKeFoTmvFho9xFiJAAvmwmtKphLuJ9Hq4K","type":"free","name":"teste","__v":0,"visible":true,"locations":[{"latitude":65.9667,"longitude":-18.5333,"_id":"55e064e9727b44c32a262c10","timestamp":"2015-08-28T13:40:57.572Z"}]}

Maybe my eyes are too tired, but I simply cannot figure this out, does anybody see something obvious that I am missing here?

Update: I put this into /, the problem is, it is working there, but not here (iojs). Should there be any difference?

I am trying to delete an object's property but the property does not get deleted at all...

What I have:

var tagFound = yield tags.findById(this.params.tagId);
debug('prior delete: %j', tagFound);
delete tagFound.password;
debug('after delete: %j', tagFound);

What I get:

api_v1 prior delete: {"_id":"55e064e9727b44c32a262c0f","expires":"2015-08-29T13:40:57.673Z","password":"$2a$08$hucJyHIU5gholAB1L.wVKeFoTmvFho9xFiJAAvmwmtKphLuJ9Hq4K","type":"free","name":"teste","__v":0,"visible":true,"locations":[{"latitude":65.9667,"longitude":-18.5333,"_id":"55e064e9727b44c32a262c10","timestamp":"2015-08-28T13:40:57.572Z"}]}

api_v1 after delete: {"_id":"55e064e9727b44c32a262c0f","expires":"2015-08-29T13:40:57.673Z","password":"$2a$08$hucJyHIU5gholAB1L.wVKeFoTmvFho9xFiJAAvmwmtKphLuJ9Hq4K","type":"free","name":"teste","__v":0,"visible":true,"locations":[{"latitude":65.9667,"longitude":-18.5333,"_id":"55e064e9727b44c32a262c10","timestamp":"2015-08-28T13:40:57.572Z"}]}

Maybe my eyes are too tired, but I simply cannot figure this out, does anybody see something obvious that I am missing here?

Update: I put this into http://jsfiddle/fc8mohwp/, the problem is, it is working there, but not here (iojs). Should there be any difference?

Share Improve this question edited Aug 28, 2015 at 17:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 28, 2015 at 14:03 rbapradorbaprado 973 silver badges8 bronze badges 5
  • 1 what does delete return? – vault Commented Aug 28, 2015 at 14:09
  • 1 Where does tagFound e from? If the password field es from a HTML-Tag than it cannot be deleted. Only user defined properties can be deleted. At least that's the case in a browser but probably something similar is happening in node. – Daniele Torino Commented Aug 28, 2015 at 14:09
  • Delete returns true. tagFound es from a mongoDB document – rbaprado Commented Aug 28, 2015 at 14:14
  • The object property may not be writable. – Pointy Commented Aug 28, 2015 at 14:16
  • please remove node.js tag and insert io.js – vault Commented Aug 28, 2015 at 14:18
Add a ment  | 

3 Answers 3

Reset to default 4

Certain variables cannot be deleted depending on the context of how they are declared. For example, you can't delete variables that have been defined in a global scope.

I hate to post a link answer, but this blog post describes all the rules and edge cases with using delete. It is quite a bit for me to include in a SO post

Using delete in Javascript

This can have multiple reasons for example the password field can't be changed or the password field is ing from the prototype chain.

You can debug this with Object.getOwnPropertyDescriptor(tagFound, 'password');

You can delete a property from a javascript object by using the delete operator

delete myObject.proportyname;

发布评论

评论列表(0)

  1. 暂无评论