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