4

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.net/fc8mohwp/, the problem is, it is working there, but not here (iojs). Should there be any difference?

5
  • 1
    what does delete return? Commented Aug 28, 2015 at 14:09
  • 1
    Where does tagFound come from? If the password field comes 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. Commented Aug 28, 2015 at 14:09
  • Delete returns true. tagFound comes from a mongoDB document Commented Aug 28, 2015 at 14:14
  • The object property may not be writable. Commented Aug 28, 2015 at 14:16
  • please remove node.js tag and insert io.js Commented Aug 28, 2015 at 14:18

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

tagFound is declared locally, but it comes from an yield statement, could that be the problem?
@rbaprado Possible. Where is tags.findById coming from? Is this a DOM element that your trying to delete?
tags.findById is a wrapper for mongoose's findOne, no DOM here
@rbaprado Gotcha. Hard to say then. The mongoose library might be doing something goofy to try and protect it's objects. You could try doing a deep copy of the object by converting it to and from a JSON string. That might allow you to declare a local copy of the variable which would allow you to delete it.
As ugly as it gets, tagFound = JSON.parse(JSON.stringify(mobtagFound)) did the trick, thanks!
1

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

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

1 Comment

Then it is coming from the prototype.
-3

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

delete myObject.proportyname;

1 Comment

delete is an operator, not a function. Therefore this should be delete myObject.propertyname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.