简体   繁体   English

未捕获(承诺中)错误:使用类型的操作调用时

[英]Uncaught (in promise) Error: When called with an action of type

Not sure what I am missing... The request is okay and I can see the data when I console.log in the action creator but for some reason the reducer does not work properly.不知道我错过了什么......请求没问题,当我 console.log 进入动作创建者时我可以看到数据,但由于某种原因,reducer 无法正常工作。

Here is the code:这是代码:

Action:行动:

import jsonPlaceholder from "../apis/jsonPlaceholder";

export const fetchPosts = () => {
  return async (dispatch) => {
    const response = await jsonPlaceholder.get("/posts");

    dispatch({ type: "FETCH_POSTS", payload: response.data });
  };
};

//// ////

jsonPlaceHolder.js jsonPlaceHolder.js

import axios from "axios";

export default axios.create({
  baseURL: "https://jsonplaceholder.typicode.com/",
});

//// ////

Reducers: fetchData.js减速器:fetchData.js

const fetchData = (state = [], action) => {
  switch (action.type) {
    case "FETCH_POSTS":
      return action.payloads;
    default:
      return state;
  }
};

export default fetchData;

index.js index.js

import { combineReducers } from "redux";
import fetchData from "./fetchData";

export default combineReducers({
  post: fetchData,
});

/// ///

const mapStateToProps = (state) => {
  console.log(state);
  return { posts: state };
};

export default connect(mapStateToProps, { fetchPosts })(PostList);

The error:错误:

Uncaught (in promise) Error: When called with an action of type "FETCH_POSTS", the slice reducer for key "post" returned undefined.未捕获(承诺中)错误:当使用“FETCH_POSTS”类型的操作调用时,键“post”的切片缩减器返回未定义。 To ignore an action, you must explicitly return the previous state.要忽略某个操作,您必须显式返回之前的状态。 If you want this reducer to hold no value, you can return null instead of undefined.如果你希望这个 reducer 不保存任何值,你可以返回 null 而不是 undefined。

const fetchData = (state = [], action) => {
  switch (action.type) {
    case "FETCH_POSTS":
      return action.payload; //fixed
    default:
      return state;
  }
};

export default fetchData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM