[英]Nested Array in JSON: Push / Unshift with date object creates double entry
I'm experiencing a weird behaviour with JavaScript. 我在使用JavaScript时遇到了奇怪的行为。 I am trying to construct a JS object like this in JavaScript (node.js/express)
我正在尝试在JavaScript(node.js / express)中构造这样的JS对象
var docAudit = {date: nowObj, changed: {}};
docAudit.changed['N5'] = 'DieterDorian';
console.log(docAudit) prints out now as expected: console.log(docAudit)现在可以按预期打印:
{ date: Thu Sep 04 2014 11:00:00 GMT+0200 (CEST),
changed: { N5: 'DieterDorian' } }
Now I want to push or unshift this docAudit object into my 'historie' Array in my 'ip' JSON (fetched from mongoDB) 现在,我想将此docAudit对象推送或取消移动到“ ip” JSON中的“ historie”数组中(从mongoDB中获取)
ip: {
__v: 15
_id: "53fb42bf52b6542527cb7d23"
aktuell: {
N5: "HansHerschel"
Auftragsnr: "2018"
}
historie: [
{
date: "2014-09-04T09:53:20.533Z"
changed: {
N5: "HansHerschel"
}
]
}
Now this operation: 现在这个操作:
ip.historie.unshift(docAudit);
should result in adding the docAudit object in front of the historie array inside the 'ip' JSON object. 应该会导致将docAudit对象添加到“ ip” JSON对象内部的historie数组的前面。 However what happens is this:
然而,这是怎么回事:
ip: {
__v: 16
_id: "53fb42bf52b6542527cb7d23"
aktuell: {
// not important what happens here
}
historie: [
{
date: "2014-09-04T11:00:00.533Z"
changed: {
N5: "DieterDorian"
},
{
date: "2014-09-04T11:00:00.533Z"
},
{
date: "2014-09-04T09:53:20.533Z"
changed: {
N5: "HansHerschel"
}
]
}
So what happens is an additional Date object gets pushed into the 'historie' array. 因此,发生了什么事,一个附加的Date对象被推入了“ historie”数组。 I am thinking I might constructing the 'docAudit' object wrongly.
我认为我可能错误地构造了“ docAudit”对象。 Expected output is just to push the docAudit into the array without an additional date object.
预期的输出只是将docAudit推入数组而没有其他日期对象。
Or if I print out the 'ip' object with console.log(ip)
: 或者如果我用
console.log(ip)
打印出“ ip”对象:
{ changed: {}, date: Thu Sep 04 2014 11:48:51 GMT+0200 (CEST) },
{ date: Thu Sep 04 2014 11:48:51 GMT+0200 (CEST),
changed: [Object] } ] }
This is my routing code: 这是我的路由代码:
updateIP = function(req, res) {
return IP.findById(req.params.id, function(err, ip) {
if(!ip) {
res.statusCode = 404;
return res.send({ error: 'Not found' });
}
var docAudit = {'changed':{}, 'date': nowObj};
docAudit.changed.N5 = 'DieterDorian'
ip.historie.unshift(docAudit);
ip.markModified('aktuell');
ip.markModified('historie');
return ip.save(function(err) {
if(!err) {
console.log('Updated');
return res.send({ status: 'OK', ip:ip });
} else {
if(err.name == 'ValidationError') {
res.statusCode = 400;
res.send({ error: 'Validation error' });
} else {
res.statusCode = 500;
res.send({ error: 'Server error' });
}
console.log('Internal error(%d): %s',res.statusCode,err.message);
}
res.send(ip);
});
});
};
Update: 更新:
I think the question is whether the following broken-down code is correct: 我认为问题在于以下细分代码是否正确:
updateIP = function(req, res) {
return IP.findById(req.params.id, function(err, ip) {
// alter the ip JSON here...
ip.save(function(err) {
if(!err) {
return res.send({ status: 'OK', ip:ip });
}
}
});
}
First of all, your 'ip' structure is not proper, i have corrected it: 首先,您的“ ip”结构不正确,我已对其进行了更正:
ip:
{
__v: 15,
_id: "53fb42bf52b6542527cb7d23",
aktuell: {
N5: "HansHerschel",
Auftragsnr: "2018"
},
historie: [
{
date: "2014-09-04T09:53:20.533Z",
changed:
{
N5: "HansHerschel"
}
}
]
};
on applying, 在申请时
var count = ip.historie.unshift(docAudit);
I get the proper expected result, and get the count as 2. 我得到了正确的预期结果,并将其计数为2。
{"__v":15,"_id":"53fb42bf52b6542527cb7d23",
"aktuell":{"N5":"HansHerschel","Auftragsnr":"2018"},
"historie":[
{"date":"x","changed":{"N5":"DieterDorian"}},
{"date":"2014-09-04T09:53:20.533Z","changed":{"N5":"HansHerschel"}}
]}
Check if you are doing two upshifts. 检查您是否正在两次换高档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.