I want to get the value of a nested object within an array. I get the _id of the parent object, as well as the _id of the child object within the array, but I am not able to retrieve the value of "solvedOn" to toggle a checkbox (behavior).
So this is an example object with its nested objects:
{
_id: new ObjectId("6292722ea8173377658171ff"),
title: 'work-list',
tasks: [
{
name: 'go to work',
createdOn: '2022-05-28T19:04:14.799Z',
solvedOn: false,
_id: new ObjectId("6292722ea8173377658171f8")
},
{
name: 'do some work',
createdOn: '2022-05-30T17:20:56.272Z',
solvedOn: false,
_id: new ObjectId("6294fdbf8717c09237c5098e")
}
],
__v: 0
}
So I already went this way:
List.findOne(
{_id: listID},
{tasks: {
$elemMatch: {
_id: taskID
}
}},
(err, res) => {
console.log(res.tasks);
}
)
When I try to log "res.tasks.solvedOn" then the result is 'undefined'. Even though, when I use the placeholder: "res.tasks.$.solvedOn"
This is what I get back:
[
{
name: 'go to work',
createdOn: '2022-05-28T19:04:14.799Z',
solvedOn: false,
_id: new ObjectId("6292722ea8173377658171f8")
}
]
So how I can work with the value of "solvedOn" to toggle the status of a checkbox?
Like:
If (true) { // set value to false } else {// set value to true }