I am trying to update this array object:
I am trying to make it in this way
static async InsertAnwer(product, answer, questionId) {
let newAnwers = []
try {
product.secao.forEach(element => {
if(element._id == questionId && element.answers.length > 0) {
newAnwers.push(...element.answers)
newAnwers.push(answer)
element.answers = newAnwers
}else if(element._id == questionId) {
newAnwers.push(answer)
element.answers = newAnswers
}
})
await product.save()
return product
}catch(err) {
console.log(err)
return new EndMsg(500, err)
}
}
The code is going into the secound "IF", but anyways the answers array in Mongo Object is not updating
2
Answers
If you are using mongoose, you an do a direct query for that using the $push operator:
Answer