-1

[PUT 404 (NotFound) ][1]

Client-side code

const confirmDeliver = (event) => {
const newQuantity = inventory.quantity - 1;
const updateQuantity = { newQuantity };
const url = `http://localhost:5000/inventory/${inventoryId}`;
fetch(url, {
  method: "PUT",
  headers: {
    "content-type": "application/json",
  },
  body: JSON.stringify(updateQuantity),
})
  .then((response) => response.json())
  .then((data) => console.log(data)); };
  

Server-side code

   app.put("inventory/:id", async (req, res) => {
  const id = req.params.id;
  const updatedQuantity = req.body;
  const filter = { _id: ObjectId(id) };
  const options = { upsert: true };
  const updatedDoc = {
    $set: {
      quantity: updatedQuantity.quantity,
    },
  };
  const result = await inventoryCollection.updateOne(
    filter,
    options,
    updatedDoc
  );
  res.send(result);
});

Can anyone tell me why I am getting this error? How can I solve this?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • How do you expect anyone to be able to tell you why that URL is giving a 404 error? You haven't shown any of the server side code that you expect to handle it. – Quentin May 06 '22 at 17:34
  • In the image you got an error that say /inventory/{id} `PUT` not found. to catch that error add the .catch() closure to your code. – Martinez May 06 '22 at 17:37
  • @Jackkobec — You suppose wrong. The error message shows the URL and the value is in it. – Quentin May 06 '22 at 17:40
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 06 '22 at 18:04
  • Does this answer your qeustion? https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express – Drew Reese May 06 '22 at 18:24
  • 1
    Typo: Your route path needs to be `app.put("/inventory/:id"`with a `/` at the front. – Quentin May 07 '22 at 06:21

1 Answers1

-1

‘Unexpected token <‘ means the response returned is not a valid json. This error may be from a 404 error html page responded from backend.

The request method PUT is different from request methods GET, POST. Make sure the backend has a PUT request method with an appropriate endpoint http://localhost:5000/inventory/your-inventory-id.

To fast test a SUCCESS connection to backend, simply code in backend to return an empty {} json response.

mondayrris
  • 539
  • 4
  • 13