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

86
Vistas
Mongoose / MongoDB query

help me, I am stuck at working on a functionality which helps a user to book a limited seats in any hall. So my logic is that, I generate a document every time i got input{no of seats to be booked}. So Initially if no of documents is zero than all the seats are available and seat counter starts with s1 which gives each seat unique identification. All this going right but when I give Input for the very first time it says no seats available and in the second input it makes document with number of seats booked{input number} and array containing seat numbers generated from "S1" and goes up-to limited number given. After this the next problem came is after giving input third time it again generate document with S1 and which is already generated in document before. After this i gave any no of inputs it goes fine until limit exceed. Here is my code.

 const { name, seats, message } = req.body;
  var query = reserve.find({});
  query.count((err, count) => {
    if (err) throw err;
    if (count === 0) {
      seatStart = 1;
      availableSeats = 80;
      count++;
    }
  });

  if (availableSeats >= seats && seats > 0) {
    var seatNo = [];
    for (var i = 0; i < seats; i++) {
      seatNo.push("S" + seatStart);
      seatStart++;
    }
    availableSeats -= seats;
    mong.Reserve.create(
      {
        name: name,
        seats: seats,
        seatNo: seatNo,
        message: message,
      },
      (err, data) => {
        if (err) throw err;
        console.log(data);
      }
    );
    res.send("success");
  } else {
    if (availableSeats == 0) {
      res.send("not available !! all booked");
    } else {
      res.send("not available !! only " + availableSeats + " seats available");
    }
  }

Output Output

about 4 years ago · Juan Pablo Isaza
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