如何通过 POST、DELETE 和 GET 请求使用多个提取

How to use multiple fetchs with POST, DELETE and then GET requests

我在单击时使用提取将列表中的一个对象移动到另一个对象时遇到问题。首先,我正在执行 POST 请求而不是 DELETE,最后,我使用 GET 来更新 React 中的状态。但它不能正常工作,因为状态没有更新。如何解决?

const handleFormAccept = (id: any) => {
        console.log(id);
        fetch(
            `api/forms/${currentUserData.name}/${currentUserData.date}/${currentUserData.email}/${currentUserData.phone}`,
            {
                method: "POST",
            }
        ).then(() =>
            fetch(`api/forms/${id}`, {
                method: "DELETE",
            }).then(() =>
                fetch("api/forms")
                    .then((res) => res.json())
                    .then((data) => {
                        setFormsData(data);
                    })
            )
        );
    };

尝试使用 async-await 它现在更具可读性和更容易理解你在做什么我只是安慰值可以根据那个设置

  const handleFormAccept = async (id: any) => {
    try {
      const postRes = await fetch(
        `api/forms/${currentUserData.name}/${currentUserData.date}/${currentUserData.email}/${currentUserData.phone}`,
        {
          method: 'POST',
        },
      );
      console.log({ postRes });
      const deleteRes = await fetch(`api/forms/${id}`, {
        method: 'DELETE',
      });
      console.log({ deleteRes });

      const getRes = await fetch('api/forms');
      console.log({ getRes });
    } catch (error) {
      console.log(error);
    }
  };