Add a new attribute to existing json object in node.js

后端 未结 7 1786
梦如初夏
梦如初夏 2021-01-17 07:16

I have an object like this

==================records=========={ Id: 5114a3c21203e0d811000088,
  userId: \'test\',
  sUserId: test,
  userName: \'test\',
  ur         


        
7条回答
  •  鱼传尺愫
    2021-01-17 07:58

    I am assuming you are trying to add a property to a returned Mongoose Document to reuse it somewhere else. Documents returned by Mongoose are not JSON objects directly, you'll need to convert them to an object to add properties to them. The following should work:

    //... record is a mongoose Document
    var r = record.toObject();
    r.Name = 'test';
    console.log("Record ",r);
    

提交回复
热议问题