I'm experiencing a weird behaviour with JavaScript. I am trying to construct a JS object like this in JavaScript (node.js/express)
var docAudit = {date: nowObj, changed: {}};
docAudit.changed['N5'] = 'DieterDorian';
console.log(docAudit) prints out now as expected:
{ 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)
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. 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. I am thinking I might constructing the 'docAudit' object wrongly. Expected output is just to push the docAudit into the array without an additional date object.
Or if I print out the 'ip' object with console.log(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:
{
__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.
{"__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.