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

178
Visualizações
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 Respostas
Responde à pergunta

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