Unable to add properties to js object

前端 未结 1 1509
野的像风
野的像风 2020-12-03 04:34

I am returning a Mongoose document and wish to add some meta data to it before I send it off. I am however unable to add any properties and I am not sure why. I have checked

相关标签:
1条回答
  • 2020-12-03 05:16

    Ah.. My object is a Mongoose document which doesn't allow adding properties. The solution is to either convert the returned document to a plain object or to call lean() in the query.

    Item.findById(req.params.id).exec(function(err, doc) {
      var obj = doc.toObject();
      ...
    });
    
    Item.findById(req.params.id).lean().exec(function(err, doc) {      
      ...
    });
    
    0 讨论(0)
提交回复
热议问题