I feel like there's some code I'm doing wrong but I can't figure out what, This code:
////Delete an answer
router.post('/:questionID/answer/:answerID', async(req,res)=>{
try{
let result = await Question.findByIdAndUpdate(req.params.questionID,{$pull:{Answers:{_id:req.params.answerID}}},
{safe: true, upsert: true},
function(err, question) {
if(err){
console.log(err);
}else{
console.log(question.Answers)
}})
res.send(result)
}catch(err){
res.send(err)
}})
A result is an empty object, but I am able to access the data necessary. I'm trying to access one of the answers is the "Answers" array, and then delete it from the array, and reupload the question object as a post request.
> {
"Answers": [
{
"_id": "5fac86564ea3959fe831e339",
"Answer": "Hello this is a fifth answer",
"Votes": 1,
"date": "2020-11-12T00:48:22.065Z"
},
{
"_id": "5fac86734ea3959fe831e33a",
"Answer": "Hello this is a sixth answer",
"Votes": 1,
"date": "2020-11-12T00:48:51.206Z"
}
],
"Subjects": [
"Canvas",
"Google",
"LTIS",
"Course set up"
],
"_id": "5fac6603b14deb7c847dd9f6",
"Question": "How do I connect google?",
"date": "2020-11-11T22:30:27.835Z",
"__v": 0
},
The full get request is
http://localhost:8080/questions/5fac6603b14deb7c847dd9f6/answer/5fac86564ea3959fe831e339
I've seen a lot online about how $pull is the correct answer, but I can't really tell if I'm actually editing the original object or the correct data. Any wisdom is appreciated.
Edit:No errors are being thrown in this process.