How to use mongoose to update user profile info How to use mongoose to update user profile info mongoose mongoose

How to use mongoose to update user profile info


findOneAndUpdate method on User returns promise and another flavor where it accepts callback. You did not pass any which caused this error. You can try callback approach or one below written.

userRouter.post('/update/:username', async (req, res) => {    const update = {age: req.body.age, height: req.body.height, weight: req.body.weight, gender: req.body.gender}    const filter = {username: req.params.username}    const updatedDocument = await User.findOneAndUpdate(filter, update, { new: true });    return res.status(200).send(updatedDocument);  });


In REST API specification when updating some resources on the server, you have to use the PATCH or PUT method for the request.

You using the POST method.

Here you can do this way

First, get a username and search in the database using filter property.Now get req.body and apply updated data

app.patch('/update/:username', (req, res) => {  //You can pass req.body directly or you can separate object  const { age, height, weight, gender } = req.body;  const { username } = req.params;  const filter = { username : username }  const updatedUser = await User.findOneAndUpdate(filter, req.body, { new: true }).catch(error => {    return res.status(500).send(error);  });  return res.status(200).json({    message : "Updated user",    data: updatedUser  });});