gpt4 book ai didi

node.js - 向 node.js 中的现有 json 对象添加新属性

转载 作者:IT老高 更新时间:2023-10-28 13:03:54 26 4
gpt4 key购买 nike

我有一个这样的对象

==================records=========={ Id: 5114a3c21203e0d811000088,
userId: 'test',
sUserId: test,
userName: 'test',
url: 'test',
Title: 'test'
}

我需要在上面的记录中添加一个新字段Name : 'test',我尝试给records.Name = name,它没有工作。

请帮忙

谢谢,小伙伴们

最佳答案

我假设您正在尝试将属性添加到返回的 Mongoose 文档以在其他地方重用它。 Mongoose 返回的文档不是直接的 JSON 对象,您需要将它们转换为对象以向它们添加属性。以下应该有效:

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

关于node.js - 向 node.js 中的现有 json 对象添加新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14768132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com