axios req 失败错误:请求失败,在 XMLHttpRequest.handleLoad 处的 createError (createError.js:16) at 结算 (settle.js:17) 处的状态码为 404

时间:2021-03-14 13:59:23

标签: javascript react-redux axios redux-thunk redux-toolkit

我的补丁请求不起作用。当我在没有 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

1 个答案:

答案 0 :(得分:1)

查看错误图像后,它显示数据中的某些内容正在以 undefined 的形式发送 我建议使用 console.log 并检查发送的未定义内容并尝试解决该问题或使用其他一些信息更新问题。 enter image description here