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

163
Vistas
MongooseError: Query was already executed:

I'm trying to update the document but the error says the query has already been executed.

MongooseError: Query was already executed: footballs.updateOne({ date: 'January 4' }, {})

app.post('/api/bookslot', async (req, res) => {
  console.log(req.body);
  try {
    const token = req.headers['x-access-token'];
    const decoded = jwt.verify(token, 'secret123');
    const email = decoded.email;
    const user = await UserModel.findOne({ email: email });

    let sportname = req.body.selectedSport.toLowerCase();
    const time = req.body.slotTime;
    const seats = req.body.availableSeats - 1;

    if (!sportname.endsWith('s')) {
      sportname = sportname.concat('s');
    }
    const NewSlotModel = mongoose.model(sportname, slotSchema);
    var update = {};
    update[time] = seats - 1;
    console.log(update);
    const a = await NewSlotModel.updateOne(
      { date: req.body.slotDate },
      { $set: update },
      function (err, success) {
        if (err) return handleError(err);
      }
    );

    return res.json({ status: 'ok' });
  } catch (e) {
    console.log(e);
    res.json({ status: 'error' });
  }
});

where am I going wrong?

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

0

You are using both async/await and callbacks in your code, causing mongoose to throw an error. The actual effect of using them both is exactly the error type that you are receiving:

Query was already executed

Mongoose v6 does not allow duplicate queries.

Mongoose no longer allows executing the same query object twice. If you do, you'll get a Query was already executed error. Executing the same query instance twice is typically indicative of mixing callbacks and promises, but if you need to execute the same query twice, you can call Query#clone() to clone the query and re-execute it. See gh-7398

Duplicate Query Execution

To fix the issue, just remove the third argument from the await

NewSlotModel.updateOne

Making it:

const a = await NewSlotModel.updateOne(
  { date: req.body.slotDate },
  { $set: update }
);
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