Search code examples
javascriptmysqlaxiosputendpoint

Endpoint URL not found in axios PUT endpoint


When pressing a button, my app is suppose to subtract 1 from the current Quantity in the database where the name of the item is "CrossBag"

My PUT endpoint on the client:

confirm = () => {
    axios.put(`http://localhost:5000/merch/CrossBag`, {
        Quantity: this.state.merchInfo[0].Quantity - 1,
        });
};

The structure of my table merch where Name is the primary key: enter image description here

The server side of the endpoint:

app.put('/merch/:name', function (req, res) {
  const { name } = req.params;
  const { Quantity } = req.body;

  connection.getConnection(function (err, connection) {
     if(err) {
         console.log(err)
         return
     }

     connection.query(`UPDATE merch SET Quantity = ${Quantity} WHERE name = ${name}`, function (error, results, fields) {
       
      if (error)
      {
        throw error;
      }
    });
   });
});

When I press the button though it states:

PUT http://localhost:5000/merch/CrossBag 404 (Not Found)

I'm a bit confused on how to update specifically that row's Quantity. I read that you need some kind of ID to specify which row to change, so I made that ID my Primary Key (Name).

Can anyone provide me any guidance?


Solution

  • You need to define the route like below:

    // this will match route like /merch/CrossBag or /merch/Blanket
    app.put('/merch/:name', function (req, res) {
      const {name = '' } = req.params; 
      connection.getConnection(function (err, connection) {
         if(err) {
             console.log(err)
             return
         }
       });
    });