Cannot get data from backend while updating

Viewed 20

Here's the code in react that I am using to get the data from database.

const getData = async (e) => {
    const res = await fetch(`${process.env.REACT_APP_BASE_URL}/edit/${id}`, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
    });

    const data = await res.json();
    console.log(data);

    if (res.status === 422 || !data) {
      console.log("Error");
    } else {
      setValues(data);
      console.log("Data Edited successfully");
    }
  };

  useEffect(() => {
    getData();
  }, []);

Here's the patch request

router.patch("/edit/:id", async (req, res) => {
  try {
    const { id } = req.params;

    const updateUser = await Crud.findByIdAndUpdate(id, req.body, {
      new: true,
    });

    console.log(updateUser);
    res.status(201).json(updateUser);
  } catch {
    res.status(422).json(error);
  }
});

I want to update the data in my application but I cannot get the data from the database. So can anyone tell what the problem is

1 Answers

enter image description here

From frontend, you are calling GET request and from your backend, you're receiving as a patch how it works pls do the same method on both hands

Related