Response doesn't return updated document

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}}}

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?

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) {

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 :smiley:

I tried that and it’s still the same :sweat_smile:

        const addedPost =  await NewUser.updateOne(
        {_id:token._id},{$push:{posts: {content:req.body.content}}},(err,data)=>{

What if you change to:

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 :confused:

Otherwise, hopefully someone else can help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.