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

246
Visualizações
How to subtract a value from an array in node-js?

Suppose I have a very simple array like this totalSpots = [95] and it has only one value. Now a new booking is created and I want to automatically assign 1 parking-spot to the user whoever booked it and reduce array's value by 1 or whatever the user enter to make it [94] or [95 - user_request_number]

Below is the query to create a booking:

exports.createBooking = async (req, res) => {
  try {
    const user = await Auth.findOne({ _id: req.data.id });
    if (!user) return res.status(404).json({ error: "User not found" });

    const { parkingId, date, startTime } = req.body;
    
    const parking = await Parking.findOne({ _id: parkingId, date });

    if (!parking)
      return res.status(404).json({ error: "Parking details not available" });

    const bookingDetails = new Booking({
      user,
      parking,
      startTime,
      duration: req.body.duration,
      date: moment(req.body.date).format("MMM DD, YYYY"),
      endTime: req.body.endTime,
      paymentAmount: req.body.paymentAmount,
      isFeePaid: req.body.isFeePaid,
      status: "sent",
    });

    const save = await bookingDetails.save();

    return res.status(200).json({
      success: true,
      msg: "Service Booked",
      data: { details: save },
    });
  } catch (error) {
    return res.status(500).json({ error: error.message });
  }
};

Currently I am not focusing on taking a spot values from users but I will do in the future.

And when a consumer removes his vehicle from that place, I want to increase a value as well, but not higher than the totalSpots that the merchant has set.

I am storing totalSpots as an array because in the future the value could be like this totalSpots = [1, 2, 3 ...upto 95]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I believe you need to rethink in your solution, if you save this information in an array, the information will be saved in the memory, if your server restarts, you are going to loose this info. You should control this in the database. For example, suppose we have this merchant:

{ id: 1, name: "Merchant 1", capacity: 50 } 

And we have this bookings:

{ id_merchant: 1, User: "Paul" }
{ id_merchant: 1, User: "John" }
{ id_merchant: 1, User: "mary" }

You subtract the merchant capacity (50) by the count of bookings it has (3), we have 47 free slots yet. And you add logic so what the sytem doesn't allow to book over the capacity

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