无法在 then 块中使用 await 来调用另一个 API in react

Not able to use await in then block to call another API in react

我正在呼叫一个 API.in API 呼叫的 then 部分,我正在呼叫另一个 API。第一个 API 的输出将传递给另一个 API.

await axios
  .post(process.env + '/certificates/upload', {
      "name": "Shruti"
    }
  })
.then((response: any) => {
      filenames = JSON.stringify(response.data.office);

      axios // Not able to write async here
        .patch(process.env + "/certificates/", {
          file: filenames
        })
        .then(function(response: any) {
          alert(' Record updated successfully');
        })
        .catch((err: any) => {
          alert('Error in updating the record');
        });

我无法在第二个 API 调用中使用 await。我应该把 async 放在哪里才能在第二个 API 调用中使用 await?第一次通话工作正常。也有没有更好的方法来调用连续 API 并将第一次调用的输出传递给第二次调用。

找到包含您想要的语句的函数 await。将 async 添加到该函数的开头。

更一般地说,避免将 async/await 与链式 .then/.catch 混合使用。

我想这就是你想要的:

try {
    let response1 = await axios.post(
        process.env + '/certificates/upload',
        { name: "Shruti" }
    )
    let filenames = JSON.stringify(response1.data.office)

    await axios.patch(
        process.env + "/certificates/",
        { file: filenames }
    )

    alert(`Update succeeded`)

} catch( error ) {
    alert(`Update failed`)
}