【发布时间】:2017-12-23 15:55:21
【问题描述】:
我正在尝试对从 mongoose/MongDB 查询返回的数据数组进行一些修改。但是,我似乎无法对数组进行任何更改。我在这里遗漏了什么明显的东西吗?
function stdSend(err, data, res){
if(err){
console.log(err);
res.send(err);
}else{
console.log('rows returned: ' + data.length);
for(var rep=0;rep<data.length;rep++){
var foo = new Date(data[rep].timestamp);
console.log(Object.isFrozen(data[rep])); <- false
console.log(Object.isSealed(data[rep])); <- false
data[rep].test = 'test'; <- test not added to data[rep]
data[rep].timestamp = foo; <- timestamp not modified
console.dir(data[rep]);
}
//console.log(data);
res.send(data);
}
}
从 Mongoose .exec 函数调用 stdSend 作为回调。回调工作正常,数据在我将它提供给 Express 中的 res.send 后进入浏览器。但是,我想通过在发送数据之前将 data[].timestamp 值转换为标准日期时间值来进行一些快速的错误测试。
但是,我尝试更改数据值的一切都失败了。 data[rep].test = 'test' 无法将测试属性添加到数据,并且尝试修改时间戳也失败。 isFrozen 和 isSealed 都返回 false。
有什么想法吗?
编辑: 我认为我在这里问的内容有些混乱。我不将数据写入数据库。这是从我试图修改的数据库查询返回的对象数组。返回的对象如下:
[ { name: 'scishow',
timestamp: 1380154343818,
funding: 42,
subs: 3500 },
{ name: 'scishow',
timestamp: 1380240748329,
funding: 42,
subs: 3520 },
{ name: 'scishow',
timestamp: 1380327152521,
funding: 42,
subs: 3554 },
{ name: 'scishow',
timestamp: 1380413558026,
funding: 43,
subs: 3579 },
{ name: 'scishow',
timestamp: 1380585807946,
funding: 43,
subs: 3638 },
{ name: 'scishow',
timestamp: 1384300959056,
funding: 52,
subs: 5 },
{ name: 'scishow',
timestamp: 1384560752617,
funding: 53,
subs: 5 },
{ name: 'scishow',
timestamp: 1384646717448,
funding: 53,
subs: 5 },
{ name: 'scishow',
timestamp: 1384819280960,
funding: 53,
subs: 5 },
{ name: 'scishow',
timestamp: 1385251243369,
funding: 53,
subs: 5753 },
{ name: 'scishow',
timestamp: 1385338257810,
funding: 53,
subs: 5779 } ]
【问题讨论】: