我在使用 TS 和 Redux 時遇到了這個錯誤,無法弄清楚它為什么會發生以及如何修復它。我正在使用 redux thunk 中間件。Redux TS 錯誤操作必須是普通物件。相反,實際的型別是:'Promise'。
似乎問題開始發生在updateUserSession(data)
以這種方式開始:
insertUserSession(data)
.then(
(res: IUserSession) => {
updateUserSession(data)
},
err => {
console.error("Insert session failed", err)
},
)
.then(() => {
//Here will continue another async action
})
export const insertUserSession = (session: IUserSession): Promise<IUserSession> => {
return axios.post("/user/" session?.userId "/session", session)
}
export const updateUserSession = (data: IUserSession) => {
return axios.put('/user/' data.userId '/session', data)
.then(
(response) => {
fetchUserSession(store.getState().user?.userData?.username)
.then(
(res) => {
if (typeof res.data === "object" && !Object.keys(res.data).length)
saveUserSession(res.data)
},
(err) => {
console.error(err);
},
)
},
(err) => {
console.error(err);
},
)
}
export const saveUserSession = (data: IUserSession) => {
return (dispatch: Dispatch<UserAction>) => {
dispatch({
type: UserType.SAVE_USER_SESSION,
payload: data,
});
}
}
uj5u.com熱心網友回復:
這是因為updateUserSession實際上是一個重擊而不是一個動作。這似乎saveUserSession是您需要使用回呼資料呼叫的操作。
請務必宣告正確的 thunk 簽名:
const updateUserSession = (data: IUserSession) => (dispatch, getState) => {}
另外,不要忘記dispatch在 thunk 中呼叫觸發動作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/430576.html
下一篇:jQueryAjax后臺行程
