My next action is to update the task object based on user’s input for the “taskName”. For example: if a user inputs “work” as “taskName”, I want the task object to be updated as "task": {work:{}}.
For that I used the method below but it never updates the “task” object. Can anyone see what I am doing wrong?
var id = "5ee8468316a2ea32bcfbf411"
var taskName="work"
var key = 'task.'+taskName
var push = {}
push[key]={}
Data.findByIdAndUpdate({_id:id}, push, (err,data)=> {
if(err) throw err
}
);
Thanks for the response. But I think that is what I tried to do by putting “push” in the findByIdAndUpdate function. As I defined it push = {task.work: {}}. Is that not the right way to update the object “task” using a variable object key?
I am not all to experienced with Mongoose, but you are not returning your altered document. Generally, this is done using the aptly-named done() callback, but you can just ensure you are returning the data once updated…
Thanks for point that out. But I have a different getData route I am using that specifically retrieves the updated data and I know the route works because when I update other parameters in the data I see the change.
I am pretty sure the problem has to do with the Mongoose function to update the data but I can’t find a documentation that shows how to update objects when you are using a variable key.