我的补丁请求不起作用。当我在没有 AsyncThunk 的情况下发出补丁请求时,它工作正常并且没有给出任何错误,但是当使用 Thunk 时它失败并给出以下错误。 在这张图片中可以看到错误
[![在此处输入图像描述][1]][1] 我正在发送 postData(蓝色下划线)。但我无法找到它显示未定义有效载荷的原因。 [![在此处输入图像描述][2]][2] 另外,我将 2 个值作为对象传递给“asyncThunk”,因为它只需要一个参数作为有效负载。这是否是问题所在! [![在此处输入图片描述][3]][3]
export const updatePost=createAsyncThunk(
'Post/updatePost',
async({id,updatePost})=>{
try{
const {data}=await api.updatePost(id,updatePost);
console.log(data)
return data;
}catch(err){
console.log('axios patch req failed',err)
}
}
)
const postSlice=createSlice({
name:'Post',
initialState:{
posts:[],
status:'failure'
},
extraReducers:{
[updatePost.fulfilled]:(state,action)=>{
console.log(action.payload)
state.status='success'
return state.posts.map((post)=>post._id === action.payload._id ?action.payload :post)
},
[updatePost.pending]:(state,action)=>{
state.status='pending'
console.log('pending due to',action)
},
[updatePost.rejected]:(state,action)=>{
state.status='rejected'
console.log('updatePost is rejected',action)
}
}
})```
** here is my patch request function **
```export const updatePost=(id,updatePost)=>axios.patch(`${url}/${id}`,updatePost)```
** dispatching my action **
```dispatch(updatePost({currentId,postData}))```
[1]: https://i.stack.imgur.com/Wb37y.png
[2]: https://i.stack.imgur.com/86a4l.png
[3]: https://i.stack.imgur.com/o0CLP.png