app.patch("/newpost", async(req,res)=>{
const token = jwt.verify(req.body.token,process.env.SECRET)
try{const addedPost = await NewUser.updateOne(
{_id:token._id},{$push:{posts: {content:req.body.content}}}
)
res.json(addedPost)
}catch(err){
res.send("Error")
}
})
When I console.log
the response from this request on the front end, I don’t get the updated document but a bunch of weird content:
What should I write to get the updated document in the response so I can grab its :_id
?
Sky020
June 6, 2021, 11:45am
#2
Hello there,
You have not said, but I assume you are using Mongoose.js?
If so, this is the defined response for updateOne
: Mongoose v5.12.13: API docs
If you want the actual updated data, you need to use the callback argument:
await updateOne(toUpdate, whatPropsToUpdate, callback);
function callback(err, data) {
console.log(data)
}
Hope this helps
1 Like
Thank you, I was really wondering why the exact same code I used as in the youtube tutorial gives the different result, this makes it clear now
I tried that and it’s still the same
const addedPost = await NewUser.updateOne(
{_id:token._id},{$push:{posts: {content:req.body.content}}},(err,data)=>{
res.send(data)
})
Sky020
June 6, 2021, 12:08pm
#5
What if you change to:
veljkocukic:
res.send(data.data)
Also, if you are not dead-set on using Mongoose, it might be worthwhile changing to just the MongoDB driver and API. For most applications, I have stopped using Mongoose, because of oddities like this
Otherwise, hopefully someone else can help.
system
closed
December 6, 2021, 12:09am
#6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.