我正在呼叫一個 API。在 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');
});
我無法await在第二個 API 呼叫中使用。我應該在哪里async使用await第二個 API 呼叫?第一個電話作業正常。還有沒有更好的方法來呼叫連續的 API 并將第一次呼叫的輸出傳遞給第二次。
uj5u.com熱心網友回復:
找到包含您想要的陳述句的函式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`)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/428812.html
標籤:javascript 反应 反应式 axios
