Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Invalid name

Viewed 777
const handleAddProduct = (e) => {
    e.preventDefault()

    const product_name = e.target.name.value;
    const image = e.target.image.value;
    const description = e.target.description.value;
    const price = e.target.price.value;
    const quantity = e.target.quantity.value;
    const supplyar_name = e.target.supplier_name.value;
    const card = {
      product_name,
      image,
      description,
      price,
      quantity,
      supplyar_name,
    };
    console.log(card);
    fetch("https://enigmatic-eyrie-33917.herokuapp.com/product", {
      method: "POST",
      headers: {
        "content/type": "application/json",
      },
      body: JSON.stringify(card),
    })
      .then((res) => res.json())
      .then((data) => {
        console.log(data);
      });
    e.target.reset();
};

whats wroon this code

error:

Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Invalid name

1 Answers

You have made mistake in this line

"content/type": "application/json"

"content-type": "application/json"

It should be like this,

fetch("https://enigmatic-eyrie-33917.herokuapp.com/product", {
  method: "POST",
  headers: {
    "content-type": "application/json",
  },
  body: JSON.stringify(card),
})

official document here

I hope you clear with this.

Related