为什么我得到的打字器"拒绝值"不是一个函数



我正在尝试使用Typescript创建AsyncThunk。在进入catch块时,我将返回rejecttwithValue(param(。但在控制台.log(操作(-我得到错误-

error:
message: "rejectWithValue is not a function"
name: "TypeError"
stack: "TypeError: rejectWithValue is not a functionn    at http://localhost:3000/main.105756d7251616c1b9a2.hot-update.js:35:12"
[[Prototype]]: Object
meta: {arg: {…}, requestId: '2Z_7_5twR1VcGC2apgQcS', rejectedWithValue: false, requestStatus: 'rejected', aborted: false, …}
payload: undefined
type: "LOGIN_USER/rejected" 

这是我的createAsynchunk

export const loginUser =  createAsyncThunk(
types.LOGIN_USER,
async (param : {payload : object, callback : Function},  rejectWithValue : any) : Promise<AxiosResponse<any, any>> => {
try{
let argum : apiArgs = {
apiUrl : '/someurl',
method : "POST",
payload : param.payload
}
const response  = await apiCall(argum)
param.callback()
return response.data

}catch(err : any){
return rejectWithValue(err.response.data)
}    
}
)

事实上,我对有效载荷很感兴趣,由于错误,我没有定义有效载荷。

参见payloadCreator,payloadCreator函数的第二个参数是具有rejectWithValue方法的thunkAPI对象。

所以,它应该是:

export const loginUser = createAsyncThunk(
types.LOGIN_USER,
async (
param: { payload: object; callback: Function },
{ rejectWithValue }: any
): Promise<AxiosResponse<any, any>> => {
try {
let argum: apiArgs = {
apiUrl: '/someurl',
method: 'POST',
payload: param.payload,
};
const response = await apiCall(argum);
param.callback();
return response.data;
} catch (err: any) {
return rejectWithValue(err.response.data);
}
}
);

相关内容

  • 没有找到相关文章

最新更新