Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

426
Vistas
Error in function to update an user [node.js]

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

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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 });
   }
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda