在 thunk 响应中变得不确定

getting undefined in thunk response

我正在使用 react with redux tookit 来调用我的 api 并将我的响应存储在状态中,但是每当我调用异步 thunk 时,我的响应都未定义,但我将我的响应记录在 api,我得到了预期的回应,我不知道因为我是 redux 的初学者,任何人都可以帮助我我做错了什么。下面是我的内存片减速器

import { RecentPublishedApi } from "../../components/api/api";

export const fetchMemoryAsync = createAsyncThunk(
  "memory/recentPublishedApi",
  async (obj) => {
    const response =await RecentPublishedApi(obj);
    console.log("i am inside the thunk",response)
    return response;
  }
);
const initialState = {
  data: [],
  status: "",
};

export const MemorySlice = createSlice({
  name: "memory",
  initialState,
  reducers: {
  },
  extraReducers: {
    [fetchMemoryAsync.pending]: (state) => {
      state.status = "loading";
    },
    [fetchMemoryAsync.fulfilled]: (state, action) => {
      console.log("fulfilled")
      state.status = "idle";
      console.log(action)
      state.data=action.payload;
    },
    [fetchMemoryAsync.rejected]: (state, action) => {
      state.status = "failed";
      state.error = action.payload;
    },
  },
});

export const selectData = (state) => state.memory.data;

export default MemorySlice.reducer;

我的代码沙盒link- https://codesandbox.io/s/hidden-snowflake-px34f?file=/src/App.js

你的 redux-thunk 看起来不错,它是你的 api 函数。

axios.post 已经 returns 一个承诺,所以将其全部包装在一个承诺中只是 returns 承诺中的承诺。

import axios from "axios";

export const RecentPublishedApi = async (data) => {
  const headers = {
    "Content-Type": "application/json",
    "X-CSRF-TOKEN": "e7lwcn_OBGJuu2QsIA8auXzsvi9RGlzueRGDDwVsSKU"
  };

  return axios
    .post("https://public.cuebackqa.com/api/timeline/list", data, {
      headers: headers
    })
    .then((res) => {
      return res.data;
    })
    .catch((err) => {
      //throw an application exception
    });
};