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

157
Vistas
MongooseError: Query was already executed: Todo.updateOne({ _id: new ObjectId("612df063a8f

I updated mongoose to latest version (6.0.2) and now I'm recieving this error and crush the application whenever .updateOne() is executed. But object update inside the database. My code:

async(req,res) => {
    await Todo.updateOne(
        {_id : req.params.id},
        {
            $set : {
                status : "inactive"
            }
        },
        (updateError) => {
            // if updateError exist
            if (updateError) {
                // print error
                printError(updateError);

                // response the error
                res.status(500).json({
                    error : "There was a Server Side Error!"
                });
            } else {
                // if error dose not exist
                // print message
                console.log("|===> 🗂️  Data Was Updated successfully! 🗂️ <====|\n");
                // response success
                res.json({
                    message : "Todo Was Update successfully!"
                });
            }
        });
    }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try to add .clone() to de updateOne function

async(req,res) => {
    await Todo.updateOne(
         {_id : req.params.id},
         {
              $set : {
                    status : "inactive"
              }
         },
         (updateError) => {
              // if updateError exist
              if (updateError) {
                    // print error
                    printError(updateError);

                    // response the error
                    res.status(500).json({
                         error : "There was a Server Side Error!"
                    });
              } else {
                    // if error dose not exist
                    // print message
                    console.log("|===> 🗂️  Data Was Updated successfully! 🗂️ <====|\n");
                    // response success
                    res.json({
                         message : "Todo Was Update successfully!"
                    });
              }
         }).clone();
    }

Latest version of mongoose not duplicate execution

https://mongoosejs.com/docs/migrating_to_6.html#duplicate-query-execution

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since you are using async syntax, put your code in try/catch blok:

async (req, res) => {
  try {
    await Todo.updateOne({ _id: req.params.id }, { $set: { status: "inactive"}});
    res.status(200).json({message: "Todo Was Update successfully!"});
  } catch (error) {
    res.status(500).json({error:'There was a Server Side Error!'})
  }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your async/await should look like this:

async (req,res) => {
  try {
      const result = await Todo.updateOne(
          {_id : req.params.id},
          {
              $set : {
                  status : "inactive"
              }
          },
          );
      }
      console.log('success', result)
      res.json({message: "Todo Was Update successfully!", result })
  } catch (err) {
     console.log('error', err)
     res.status(500).json({error:'There was a Server Side Error!'})
  }
}

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