Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!