I Want to push an object to an object inside an array on my MongoDB database. I'm trying to use $push submits[postDate] but it is giving me a red line under the first "[" . Any idea how to fix this?
My code:
app.post('/add-submit', (req,res) => {
let postDate = new Date();
let dd = String(postDate.getDate()).padStart(2, '0');
let mm = String(postDate.getMonth() + 1).padStart(2, '0');
let yyyy = postDate.getFullYear();
postDate = mm + '/' + dd + '/' + yyyy;
subject = req.body.subject
let pushValue = {
time: req.body.time,
description: req.body.description,
date: postDate
}
console.log(pushValue)
console.log(postDate)
let myquery = { username: req.body.username};
let newvalues = {
$push : {
submits[postDate] : {
[subject] : pushValue
}
}
}
db.collection("users").updateOne(myquery, newvalues, (err, response) => {
if (err) throw err;
console.log("1 document updated");
res.redirect('/users/'+req.body.id)
});
})
But i get this error:
D:\Users\willi\Documents\Node\StudyWebApp\server.js:186
submits[postDate] : {
^
SyntaxError: Unexpected token '['
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
This is what I want it to look like on the database:
Thanks.
