Home > Blockchain >  Error in function to update an user [node.js]
Error in function to update an user [node.js]

Time:10-18

I have an error when i try to update the bio on a user with postman. I use mongoDB for the database. Looks like it's a problem when i try to send the docs in the response but i don't see how to fix it

Here is the code of userController that doesn't work :

module.exports.updateUser = async (req, res) => {
    if (!ObjectId.isValid(req.params.id))
      return res.status(404).send("ID unknown : "   req.params.id);
  
    try {
      await UserModel.findOneAndUpdate(
        { _id: req.params.id },
        {
          $set: {
            bio: req.body.bio,
          },
        },
        { new: true, upsert: true, setDefaultsOnInsert: true },
        (err, docs) => {
            console.log(docs);
            if (!err) return res.send(docs);
            else return res.status(500).json({ message: err });
        }
      );
    } catch (err) {
      return res.status(500).json({ message: err });
    }
};
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

And the error is as follows:

node:events:371
      throw er; // Unhandled 'error' event
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:573:11)
    at ServerResponse.header (/home/pt/Documents/back_sport_app/api/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/home/pt/Documents/back_sport_app/api/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/pt/Documents/back_sport_app/api/node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (/home/pt/Documents/back_sport_app/api/node_modules/express/lib/response.js:158:21)
    at /home/pt/Documents/back_sport_app/api/controllers/userController.js:36:34
    at /home/pt/Documents/back_sport_app/api/node_modules/mongoose/lib/model.js:4923:18
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
Emitted 'error' event on Function instance at:
    at /home/pt/Documents/back_sport_app/api/node_modules/mongoose/lib/model.js:4925:15
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  code: 'ERR_HTTP_HEADERS_SENT'
}
[nodemon] app crashed - waiting for file changes before starting...
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You are sending the response in the callback function, but the response is already sent as soon as the promise resolves (await).

Try using -

module.exports.updateUser = async (req, res) => {
   if (!ObjectId.isValid(req.params.id))
     return res.status(404).send("ID unknown : "   req.params.id);
 
   try {
      const docs = await new Promise((resolve, reject) => {
         UserModel.findOneAndUpdate(
            { _id: req.params.id },
            {
               $set: {
               bio: req.body.bio,
               },
            },
            { new: true, upsert: true, setDefaultsOnInsert: true },
            (err, docs) => {
               if (err) {
                  reject(err)
               } else {
                  console.log(docs);
                  resolve(docs)  // resolving the promise with docs, instead of sending the response.
               }
            }
         );
      })

      return res.send(docs);  // sending response after the above promise is resolved.
   } catch (err) {
     return res.status(500).json({ message: err });
   }
};
  • Related