node.jsjsondatabaseexpressrapidapi

nodejs express PUT error and number shows in every users profile


Help please

This is how I write the code

PUT requested in 1 user

Now showing that on both users

I am trying to increase each user's "entries" number by doing PUT req. But whenever I do that for only 1 user. the number automatically increases in both users' "entries". You can understand better when you see the picture.

Thank you so much


Solution

  • In here you increasing user's entry for all the users. If you want to increase only for a specific user add the increase logic inside the if condition.

    For example like below

    app.put('/image', (req, res) => {
      const { id } = req.body;
    
      let found = false;
    
      database.users.forEach(users => {
        if (users.id === id) {
          found = true;
          users.entries++
          return res.json(users.entries);
        }
      });
      if (!found) {
        res.status(400).json('not found');
      }
    });