You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redux异步请求遭拒排查:已传Token仍提示未授权

问题根源

你的代码中createAsyncThunk的异步函数参数定义错误,导致传递给service的参数不符合预期,进而引发Token验证问题。

createAsyncThunk的回调函数仅接收两个参数:第一个是dispatch时传入的payload,第二个是thunkAPI对象。你当前的代码把payload拆成了id和data两个参数,这会导致实际传入的data是thunkAPI对象,而非你要更新的业务数据,同时后续参数传递顺序混乱,间接影响了Token的有效性验证。

修复步骤

1. 修正createAsyncThunk的参数接收方式

将slice中的updatePractitionerProfile thunk函数修改为:

// Update a practitioner profile
export const updatePractitionerProfile = createAsyncThunk(
    "practitionerProfiles/update",
    async ({ id, data }, thunkAPI) => { // 改为解构payload对象
        try {
            const token = thunkAPI.getState().auth.user.token
            console.log(token) // 可临时打印确认token是否存在
            return await practitionerProfileService.updatePractitionerProfile(
                id, token, data
            );
        } catch (error) {
            const message =
                (error.response &&
                    error.response.data &&
                    error.response.data.message) ||
                error.message ||
                error.toString();
            return thunkAPI.rejectWithValue(message);
        }
    }
);

2. 修正dispatch调用的传参方式

在组件中调用dispatch时,需将id和data打包成一个对象传入:

dispatch(updatePractitionerProfile({ id: yourProfileId, data: yourUpdateData }))

3. 额外验证(可选)

  • 确认thunkAPI.getState().auth.user.token确实能获取到有效Token,通过console.log打印验证。
  • 检查API_URL拼接是否正确,避免出现如api/profile123这类错误路径(Postman测试正常的话此问题概率较低)。
验证修复

修改后重新调用更新接口,即可正常携带Token完成授权验证,成功更新数据。

内容的提问来源于stack exchange,提问作者brandon.c

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 19:18:36