[SOLVED] Unable to update bio in mongodb moralis auth tutorial

Hi everyone, I am currently trying to implement a tutorial (https://www.youtube.com/watch?v=rMijvDtNYlQ&t=935s) where u can register a user through the moralis auth, in next with the mongodb, about eveything works just fine, but when I try to update the bio nothing happens.
I am using the same code used in the video, and I think that the part that is causing me issues is in the api/updateBio.js file.
this bit to be more precise.

import connectDB from "../../lib/connectDB"
import Users from "../../lib/models/userSchema"

export default async function updateBio(req, res) {
    const { profileId, bio } = req.body

    console.log(req.body)

    console.log(profileId, bio)
    await connectDB()

    try {
      
        await Users.findOneAndUpdate({ profileId: profileId }, { bio: bio })
        res.status(200).json({ bio })
    } catch (error) {
        res.status(400).json({ error })
        console.error(error)
    }
}

I tried to log the profileId and the bio in the api/updateBio file and it returns undefined. I also logged the req.body as you can see below.


So it seems that it is because we are trying to destructure an array ? Did anyone face the same issue ?

Maybe you can access directly those keys from that req.body object without the need to do anything else like req.body.bio

You mean doing it this way

await Users.findOneAndUpdate(

           { profileId: req.body.profileId. },

           { bio: req.body.bio }

       )

right ? if I log those values I get undefined

req.body["profileId"] these won’t work either :sweat_smile:

you can do a trick with JSON.stringify(req.body) and then JSON parse

you can also try req.body.get("profileId")

1 Like

It worked !! just had to JSON.parse the req.body.
Thank you and have a great day

1 Like