Need help with my JavaScript code! I want to adjust the date in a Date object by adding specific days and then save it. Here is what I have so far:
let orderIdDateCorrectionDict = [
{ "orderId": "2020053100", "dayCorrection": -146 },
{ "orderId": "2020053109", "dayCorrection": -146 },
{ "orderId": "2020053100", "dayCorrection": -146 },
];
migrateUp(db.Orders);
var index = 0;
function migrateUp(targetCollection) {
targetCollection.find({
OrderId: { $in: orderIdDateCorrectionDict.map(i => i.orderId) }
}).forEach(
function (order) {
order.TransDate.DateTime = order.TransDate.DateTime.addDays(orderIdDateCorrectionDict[index++].dayCorrection);
order.TransDate.Ticks = NumberLong((order.TransDate.DateTime.getTime() * 10000) + 621355968000000000);
targetCollection.save(order);
}
);
};
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
I'm encountering this error:
{ "message" : "Cannot read property 'dayCorrection' of undefined", "stack" : "script:9478:126" + "script:14:70" + "script:14:70" + "script:9475:8" + "script:9460:1" }
What adjustments should I make to update the records correctly with the day integer value?