The schema below:
const questionSchema = new Schema({
askedBy: {
type: String,
required: true
},
title:{
type:String,
required: true
},
queryType: {
type: String,
required: true
},
question:{
type: String,
required:true
},
datePosted: String,
isOpen: Boolean,
answers:{
type:Array
}
})
The code below:
app.patch("/:id" , (req,res) =>{
Question.findByIdAndUpdate(req.params.id, {answer:[req.body.answer]}, {new:true})
.then((response) => res.json(response))
.catch((err) => res.json(err));
} )
What I want to do is take the answer string that is coming from the body and use it to update the array. And the next time I do this I want that element to be appended to the array.
Try this:
Question.findByIdAndUpdate(req.params.id, {
$push: {
answer: req.body.answer
}, {new:true})
.then((response) => res.json(response))
.catch((err) => res.json(err));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.