我在将数据从我的action返回到redux时遇到了问题。 我的行动如下:
export const getUserProfile = createAsyncThunk( "user/getuserprofile", async (thunkAPI) => { var res, user; const isAuthenticated = localStorage.getItem("isAuthenticated") === "true"; var decodedToken = null; if (isAuthenticated) { const access_token = localStorage.getItem("access_token"); decodedToken = jwt_decode(access_token); user = decodedToken.sub; } try { res = await axios .get(${host}/manageAdmins/getUserProfile/${user}
) .then((response) => { return response.data; }); } catch (error) { return thunkAPI.rejectWithValue(error.response.data, { source: "API" }); } } );`
in my reducer I did as follows
` {
builder.addCase(getUserProfile.pending, (state, action) => {
console.log("pending");
});
builder.addCase(getUserProfile.fulfilled, (state, action) => {
console.log("my payload: ");
console.log(action.payload);
state.userProfile = action.payload;
});
builder.addCase(getUserProfile.rejected, (state, action) => {
console.log("rejected");
});
},`
当我控制有效负载时,它说undefined.但是当我在action部分控制我的数据时,它有一个我想要的值.问题是它不响应reducer的有效负载.当状态被满足时,是否有其他方法返回数据?我已经尝试了**fulfillWithValue(值, meta)**我已经从文档中看到.但不工作.有人请帮助我在做.这.我很感激!`
1条答案
按热度按时间pdsfdshx1#
您需要
return res
-当前您正在接收该数据,但未对其执行任何操作。