Hi All,
I have a Mongoose Schema and Model shown below:
const DataSchema = new Schema(
{
name:{type: String, default: ""},
task: {type: Schema.Types.Mixed, default: {}}
},
{ timestamps: true, _id: true, minimize: false, strict: false }
);
const Data = mongoose.model("Data", DataSchema);
Now when a user profile is created using the “name” parameter I would get the following response as an example:
{"success: true, "data":[{"name":"Eminem","_id":"5ee8468316a2ea32bcfbf411","task":{},"createdAt":"2020-06-16T04:11:47.441Z","updatedAt":"2020-06-16T04:11:47.441Z","__v":0}]}
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
}
);
I appreciate the help.