gpt4 book ai didi

javascript - Node - 无法将键/值附加到对象

转载 作者:行者123 更新时间:2023-11-30 17:24:05 26 4
gpt4 key购买 nike

使用 Node 。这很奇怪,我无法将键/值....broadcastStamp = date 附加到对象结果。我用 typeof 验证它是一个对象。没有错误……键/值根本不存在。

function broadcastSingleClient(result, event, httpObject) {
var date = new Date().valueOf();
result.broadcastStamp = date;
console.log(result);

更新:该对象来自 mongoose/mongodb 查询回调函数:

Object.keys(models).forEach(function (model) {
models[model].find({}).sort({_id: -1}).limit(1).exec(
function (err, result){
broadcastSingleClient(result[0], model+"Result", res);
});

在没有 exec() 回调的情况下在 shell 中执行此查询时,我能够附加到对象。

最佳答案

是否打算在修改后保存结果
您是否在 broadcastSingleClient 函数中使用了 Mongoose 虚拟属性?

如果不是,那么我认为您可以使用 lean,它将返回纯 js 对象 - 没有 Mongoose 对象包装它。

试试这个:

Object.keys(models).forEach(function (model) {
models[model].find({}).sort({_id: -1}).limit(1).lean().exec( // note the "lean"
function (err, result){
broadcastSingleClient(result[0], model+"Result", res);
});

关于javascript - Node - 无法将键/值附加到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24599843/

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