我有一个这样的对象
==================records=========={ Id: 5114a3c21203e0d811000088,
userId: 'test',
sUserId: test,
userName: 'test',
url: 'test',
Title: 'test'
}
我需要添加一个新的字段名:‘’到上面的记录中,我试着给出records.Name =,它不起作用。
请帮帮忙
谢谢,普拉特
发布于 2013-12-12 04:29:12
那些发现这个问题的人,OP在原始问题下面的评论中提到了这个问题的解决方案是:
records.set('Name', 'test')
这将添加一个名为Name
的新属性,其值为test
。
发布于 2013-05-05 02:39:25
我假设您正在尝试将属性添加到返回的Mongoose文档中,以便在其他地方重用它。Mongoose返回的文档不是直接的JSON对象,您需要将其转换为对象以向其添加属性。下面的代码应该可以工作:
//... record is a mongoose Document
var r = record.toObject();
r.Name = 'test';
console.log("Record ",r);
发布于 2015-03-19 04:16:47
只需使用
var convertedJSON = JSON.parse(JSON.stringify(mongooseReturnedDocument);
然后,
convertedJSON.newProperty = 'Hello!'
“你好!”可以是任何值、数字、对象或JSON对象文字。
干杯!:)
https://stackoverflow.com/questions/14768132
复制相似问题