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

169
Views
Delay in return value - nodejs & mongoose

I'm fairly new to nodejs and I'm doing a full stack developer challenge from devchallenges.io (Shoppingify). Below, I'm trying to increase the quantity value based on whether the user clicks to increase or decrease the item quantity. However, there's a slight delay between the return value from the request and the actual value in the database. It seems that the value updates immediately which is great however, the return value in the request is the previous value rather than being the current quantity value in the database.

mongoDB Database

enter image description here

enter image description here

// @route    PUT api/list/item/quantity/:id
// @desc     update item quantity
// @access   Private
router.put('/item/quantity/:id', auth, async (req, res) => {
  const { action } = req.body;
  try {
    let list = await List.findOne({ user: req.user.id });

    // find current quantity
    const item = list.items.find((item) => {
      return item._id.toString() === req.params.id;
    });

    // increase quantity
    if (action === 'increase') {
      list = await List.findOneAndUpdate(
        { 'items._id': req.params.id },
        { $set: { 'items.$.quantity': item.quantity + 1 } },
        { new: true }
      );
    } else {
      // decrease quantity
      list = await List.findOneAndUpdate(
        { 'items._id': req.params.id },
        { $set: { 'items.$.quantity': item.quantity - 1 } },
        { new: true }
      );
    }

    res.json(item.quantity);
  } catch (error) {
    console.error(error.message);
    res.status(500).send('Server Error');
  }
});

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are defining item in here:

const item = list.items.find((item) => {
      return item._id.toString() === req.params.id;
    });

At this point list is the "old" version of the object, you want to be doing the same after the update when the list object is updated and only then to return it.

// this is the original "list" item
let item = list.items.find((item) => {
   return item._id.toString() === req.params.id;
});

...
update list
...

// now "list" is updated
item = list.items.find((item) => {
   return item._id.toString() === req.params.id;
});

I will just add two additional tips to improve performance, they are mutually exclusive so you'll have to choose one of the two.

  1. in the update query add the list._id to it, If I were to guess the collection does not have an index on the items field ( and if it does it's a bad idea usually ). this means when you updated just using the item._id field it takes longer for mongo to find the object. it's quick change to both updates:
list = await List.findOneAndUpdate(
    { _id: list._id, 'items._id': req.params.id },
    { $set: { 'items.$.quantity': item.quantity - 1 } },
    { new: true }
);
  1. (my preferred option) do it in a single call, using the update arrayFilters option, like so:
const list = await List.findOneAndUpdate(
    {
        user: req.user.id,
    },
    {
        $inc: {
            'items.$[elem].quantity': action === 'increase' ? 1 : -1,
        },
    },
    {
        arrayFilters: [
            {
                'elem._id': new ObjectId(req.params.id),
            },
        ],
        new: true,
    });

const item = list.items.find((item) => {
    return item._id.toString() === req.params.id;
});

Mongo Playground

In my opinion now your route looks much better, you're also cutting down from 2 db calls to 1.

over 4 years ago · Santiago Trujillo 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!