Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda