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

247
Vistas
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 Respuestas
Responde la pregunta

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 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