redux 授权令牌未发送到后端

41zrol4v  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(134)

我正在尝试创建一个商店,我目前正在使用redux处理前端愿望清单。我正在尝试配置添加到愿望清单,但由于某种原因,我一直得到错误,我没有给予它一个令牌。其他一切都以应有的方式工作。
以下是我的添加到愿望清单服务:

const addWishItem = async (id, token) => {
  const config = {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  };

  const response = await axios.post(`${wishData}/add-wish/${id}`, config);

  return response.data;
};

这是我添加到愿望清单的切片:

export const addToWishlist = createAsyncThunk(
  "wish/addWishItem",
  async (id, thunkAPI) => {
    try {
      const token = thunkAPI.getState().auth.user.token;
      return wishService.addWishItem(id, token);
    } catch (error) {
      const message =
        (error.response &&
          error.response.data &&
          error.response.data.message) ||
        error.message;
      return thunkAPI.rejectWithValue(message);
    }
  }
);
0tdrvxhp

0tdrvxhp1#

传递一个空对象作为第二个参数(请求体)

const response = await axios.post(`${wishData}/add-wish/${id}`, {}, config);

相关问题