我在“saga”檔案中有一個函式,它呼叫 API 以從資料庫中獲取一些資訊
function* timeTableRequest(action) {
try {
console.log("checkpoint-N1")
const timetable = yield call(API.requestTimetable(action.id));
console.log("checkpoint-N2");
console.log("timetable", timetable);
yield put(timeTableRequestSuccess(timetable));
}
catch (error) { yield put(timeTableRequestError(error)) }
}
如您所見,有不同的“console.log()”來檢查我的代碼何時到達。同樣在此函式中,您可以看到它進行了 API 呼叫 -> API.requestTimetable(action.id) 此函式轉到創建此函式的 API 檔案夾:
export const requestTimetable = async (id) => {
const response = await fetch("https://xxx.xxxxxxx.com/admin/shop/" localStorage.getItem("shop-id") "/timetable/" id "/slot", {
method: "GET",
headers: { "Authorization": `Bearer ${JSON.parse(APIauth.getItem())}` }
});
if (!response.ok) { const errorResponse = await response.json(); throw new Error(errorResponse.message) }
const requestedTimetable = await response.json();
console.log("requestedTimetable", requestedTimetable)
return requestedTimetable;
};
這個函式一切順利,甚至你可以看到console.log你可以讀到這個函式的回傳。但是,當它必須回傳時,什么都不會回傳。

uj5u.com熱心網友回復:
的語法call是call(fn, ...args). 您應該傳遞一個函式及其引數。但是,您在這里實際所做的是立即呼叫該函式并將其回傳值傳遞給call.
因此,您必須更改call(API.requestTimetable(action.id))為call(API.requestTimeTable, action.id).
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/504833.html
標籤:javascript 反应 反应还原 还原传奇
