Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete is not working for javascript object in NodeJs

I have an event object which is fetched from MongoDB via Mongoose plug in. Have done some of the operation in this object and finally I want to save this object in another collection with same structure.

For this I need to delete all properties starts from '_' or '__' like '_id' and '__v'. I have written code like:

console.log(event);
console.log(delete event._id);
console.log(event);

Line 1 and 3 prints same value with _id property. Same thing works fine if I delete some other property like 'code'. here is complete event object:

{ _id: 5a51019b211d740a174a1fba,
  __t: 'EventModel',
  code: '02',
  event: 'testMe',
  source: 'user',
  target: 'cronjob',
  __v: 0,
  params: 
     [ { key: 'Nodics',
         value: 'Framework',
         _id: 5a51019b211d740a174a1fbb } ],
  log: [ 'Published Successfully' ],
  hits: 1,
  type: 'ASYNC',
  state: 'FINISHED',
  testProperty: 'Nodics Framework',
  updatedDate: 2018-01-13T17:04:15.288Z,
  creationDate: 2018-01-13T17:04:15.288Z 
}

Please help me out to understand root cause and how I can deal with this. for me it looks direct or inherited properties issue. but required experts comments.

like image 430
Himkar Dwivedi Avatar asked Jan 06 '18 18:01

Himkar Dwivedi


3 Answers

Did you try lean() when you query? like find(query).lean()... check this http://blog.sandromartis.com/2016/05/08/mongoose-lean/ This will allow you to do any operation to the object.

Other way could be extending the root object with removing unwanted properties from it. you can use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

example:

var obj = { a: 1 };
var copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
delete copy._id;//or whatever you want
console.lg(copy);//check this doesnt have the _id;

Thanks Hope this helps

like image 183
mustak ahmed Avatar answered Oct 20 '22 03:10

mustak ahmed


It looks like the reason you can't delete some of those properties is that they have been defined as "non-configurable." However, your approach is sub-optimal because it necessarily mutates its argument. Never, ever, mutate an argument. Ever.

Rather than delete properties from event, you ought to construct a new object that contains only the properties you desire.

  • you can safelist specific properties that you wish to transfer (and save)
  • you can blocklist specific properties that you don't wish to save
  • you can transfer only those properties that live directly on the object, rather than the prototype chain, using Object.getOwnPropertyNames

Libraries like lodash have sugar for doing this kind of thing:

// safelist (using lodash)
var saveableEvent = _.pick(event, ['code', 'event', 'source', 'target', 'params', 'log', 'hits', 'type', 'state', 'testProperty', 'updatedDate', 'creationDate']);

// or blocklist (using lodash)
var saveableEvent = _.omit(event, ['_id', '__t', '__v']);

// only copy object's own properties (without lodash)
var saveableEvent = Object.getOwnPropertyNames(event)
.reduce(function(out, propName) {
    return Object.assign(out, event[propName])
}, {})

// create editable copy of object, then remove undesired props
var saveableEvent = event.toObject();
delete saveableEvent._id;
like image 38
Tom Avatar answered Oct 20 '22 03:10

Tom


I recently had a similar problem. I used loadash to cut off the values I didn't need from the array, but had to use .toObject() in the fetched object so I could use it as an argument. Something like:

let resultExample = _.omit(exampleObject.toObject(), ['x', 'y', 'z']);
like image 35
Ivana Costa Avatar answered Oct 20 '22 03:10

Ivana Costa