Is data returned from Mongoose immutable?

前端 未结 3 918
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 20:03

I want to add to the return data from a mongoose query:

User.findById(userId, function(err, data) {
  if (!err) {
    data.newvar = \'Hello, world\';
  }
});         


        
3条回答
  •  天命终不由人
    2021-01-01 20:30

    As it turns out, Mongoose documents are their own special class and not standard Javascript objects. In order to get a javascript option that can be extended, you must use the toObject() method.

提交回复
热议问题