skip to Main Content

I’m sure this issue has been discovered and figured out before, but I’m having trouble with it so please be gentle. I’m self-teaching React/Node and decided to make a MERN CRUD system. I’m having issue with the update portion of the code and I’m not sure how to fix it. For the time being (I can update as necessary), my axios call is:

axios.put('/api/items/' + selectComponent, {
          Quantity: quantityInput.value
        })
          .then((response) => {
            setItems(previous => previous.map((item) => {
              if (item.Item === selectComponent) {
                return { ...item, Quantity: response.data }
              }
              else {
                return item
              }
            }))
          })
          .catch((error) => {
            console.log(error)
          });

The schema for the entries in the MongoDB is

  _id: String,
  Item: String,
  unit:String,
  Quantity:Number,
  Reorder: Number,
});

And the backend is:

const InvItem = mongoose.model('InventoryItems', schema);

app.get('/api/items/:Item', async (req, res) => {
  try {
    const item = await InvItem.findOneAndUpdate(
      { Item: req.params.Item },
    );
    if (!item) {
      return res.status(404).send({ error: 'Item not found' });
    }
    res.send(item);
  } catch (err) {
    console.log(err);
    res.status(500).send({ error: 'Internal server error' });
  }
});

app.put('/api/items/:Item', async (req, res) => {
  try {
    const items = await InvItem.findOneAndUpdate(
  { Item: req.params.Item },
  { $inc: { Quantity: -quantity } },
  { new: true }
    );
    if (!items) {
      return res.status(404).send({ error: 'Item not found' });
    }
    res.send(items);
  } catch (err) {
    console.log(err);
    res.status(500).send({ error: 'Internal server error' });
  } 
});

Anything I can think to try is always returning an AxiosError500 and I’m not sure what I’m doing. Be gentle, I’m new….

2

Answers


  1. From the looks of it, your quantity should be coming out of the body of the request. You might need to change

    { $inc: { Quantity: -quantity } },
    

    to

    { $inc: { Quantity: -req.body.Quantity } },
    
    Login or Signup to reply.
  2. Try to cast Quantity:

    app.put('/api/items/:Item', async (req, res) => {
      try {
        const { Quantity } = req.body;
        if (isNaN(+Quantity)) {
          return res.status(404).send({ error: 'Quantity must be a number' });
        }
        const items = await InvItem.findOneAndUpdate(
          { Item: req.params.Item },
          { $inc: { Quantity: -parseInt(Quantity) } },
          { new: true }
        );
        if (!items) {
          return res.status(404).send({ error: 'Item not found' });
        }
        res.send(items);
      } catch (err) {
        console.log(err);
        res.status(500).send({ error: 'Internal server error' });
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search