React Fetch返回未定义,但在控制台中它返回响应

pol*_*ova 0 post fetch reactjs

我可以在第一个控制台中看到我的响应,但始终是undefined

fetch("/api/v1/legacy/tenant-shop", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify(dataToSubmit)
})
  .then(response =>
    console.log("THIS RESPONSE IS WORKING, IT CONSOLE 201", response.status)
  )
  .then(response => {
    if (response && response.status === 201) {
      console.log("BUT IT NEVER GOES THERE");
      this.props.router.push("/congratilations");
    } else {
      console.log("THE RESULT OF THIS CONSOLE IS NULL", response && response);
    }
  });
Run Code Online (Sandbox Code Playgroud)

AKX*_*AKX 5

您不会从第一个返回任何东西.then(),因此下一个.then()没有任何有用的价值。

尝试

fetch("/api/v1/legacy/tenant-shop", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(dataToSubmit),
})
  .then(response => {
    console.log("THIS RESPONSE IS WORKING", response.status);
    return response;  // <---- this is important
  })
  .then(response => {
    if (response && response.status === 201) {
      console.log("BUT IT NEVER GOES THERE");
      this.props.router.push("/congratilations");
    } else {
      console.log(
        "THE RESULT OF THIS CONSOLE IS NULL",
        response && response,
      );
    }
  });
Run Code Online (Sandbox Code Playgroud)