删除nodejs中参数对象的属性

时间:2017-01-23 08:08:19

标签: javascript node.js

似乎我无法从nodejs中的另一个promise中删除对象参数中的属性

说我写

Person.save().exec().then(function(err, object){

  create(object) // Lets assume object looks like -> {person : "don"};
})

create: function(object){
  delete object.person;
  console.log(object) // It still has this name property

}
However, If I do a deep copy inside of nodejs code's create call it deleted that property.

然后相反,如果我写了像

这样的东西
function a(){ let abc = { person: "don"}; xyz(abc)}
function xyz(param){
 delete param.person;
 console.log(param); ... It logs an empty object. 
}
a()

它可以在不执行深层复制的情况下完成。这是什么原因?

1 个答案:

答案 0 :(得分:0)

我说这是由于MongoDB / Mongoose(看起来像你正在使用)对象的工作方式。

您可以通过调用.toObject()将Mongoose对象转换为常规JS对象。例如:

Person.save().exec().then(function(err, person){

  delete person.name; // won't work

  var newPerson = person.toObject(); // newPerson is now a regular JS object
  delete newPerson.name; // should work
  console.log(newPerson.name); // should return undefined
});