How To Delete An Item From Json Object Using Nodejs?
I am using node v8.11, not able to delete an item from the object which creates and returns new object from the mongoDB. Sample response after creating: { 'name': '', 'devi
Solution 1:
When you create a object using mongoose model then it will return you a model object instead of plain javascript object. So, when you do delete session._id;
it will not work as session
is a model object and it does not allow to change the property on model object directly.
You need to change the model object to plain JS object using toJSON()
or toObject()
method of model object and delete property on that:
tokens.create(req.body).then(function(session) {
var sessionObj = session.toJSON();
delete sessionObj._id;
return res.send(sessionObj);
});
Post a Comment for "How To Delete An Item From Json Object Using Nodejs?"