简体   繁体   English

React-redux 在 TypeError 中渲染帖子时出错:无法读取未定义的属性“地图”但控制台具有所有帖子

[英]React-redux Getting error while rendering post in TypeError:Cannot read property 'map' of undefined but console having all posts

As I am new to react ,I am trying to make a simple blog app using redux with react but I am facing issue while fetching the blogs.It is giving error TypeError: Cannot read property 'map' of undefined .Here is my code so please help me I m stuck here.由于我刚开始反应,我正在尝试使用 redux 和 react 来制作一个简单的博客应用程序,但是我在获取博客时遇到了问题。它给出了错误TypeError: Cannot read property 'map' of undefined 。这是我的代码请帮助我,我被困在这里。

actions/index.js动作/ index.js

export const FETCH_POSTS = 'fetch_posts';
export function fetchPosts() {
  const request = axios.get(`${ROOT_URL}/posts${API_KEY}`);

  return {
    type: FETCH_POSTS,
    payload: request
  };
}

reducers/reducer_posts.js减速器/减速器_posts.js

import { FETCH_POSTS } from '../action';
export default function (state = {post:[]}, action) {
  switch (action.type) {
  case FETCH_POSTS:
      return {
          ...state,
          post: action.payload,
      }
    default:
      return state;
  }
 }

reducers/index.js减速器/ index.js

import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import PostsReducer from './reducer_posts';

const rootReducer = combineReducers({
  posts: PostsReducer,
  form: formReducer
});

export default rootReducer;

component/posts_index.js组件/posts_index.js

class PostsIndex extends Component {
  componentDidMount() {
      this.props.fetchPosts();
  }
 render() {
      const post1=this.props
      console.log(post1)    //getting data in console
    const map_post= this.post1.map(post => <li key={post.id}>{post.title}</li>)
    console.log(this.props.posts);   // getting data here as well
    return(
      <div>
       <ul className="list-group">
      {map_post} 
        </ul>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return { posts: state.posts ,
            post1:state.posts.posts
     };
}

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

src/App.js源代码/App.js

const createStoreWithMiddleware = applyMiddleware(promise)(createStore);
function App() {
  return (
    <Provider store={createStoreWithMiddleware(reducers)}>
    <BrowserRouter>
    <div className="App">
    <Switch>
          <Route path="/posts/new" component={PostsNew} />
          <Route path="/posts/:id" component={PostsShow} />
          <Route path="/" component={PostsIndex} />
        </Switch>
    </div>
    </BrowserRouter>
</Provider>
  );
}

Also I am getting all the post in my console when I log it by commenting out the此外,当我通过注释掉

const map_post= post1.map(post => <li key={post.id}>{post.title}</li>)

but I am not able to render it in my component.I am getting error × TypeError: Cannot read property 'map' of undefined但我无法在我的组件中呈现它。我收到错误× TypeError:无法读取未定义的属性“地图”

Console Output having All post具有所有帖子的控制台输出

Thanks in advance提前致谢

Try changing your method尝试改变你的方法

 componentDidMount() {
      this.props.fetchPosts();
  }

instead of代替

 componentDidMount() {
      this.props.dispatch(fetchPosts());
  }

Also in render method add this.post to同样在渲染方法中添加 this.post

 const mappedTweets = this.post1 && this.post1.map(post => <li key={post.id}>{post.title}</li>);

暂无
暂无

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

相关问题 在执行 react-redux 项目时出现此错误。 错误:- TypeError:无法读取 React-Redux 项目中未定义的属性“地图” - Getting this error while doing react-redux project. Error:- TypeError: Cannot read property 'map' of undefined in React-Redux project 错误:类型错误:无法读取 React-Redux 中未定义的属性“map” - Error: TypeError: Cannot read property 'map' of undefined in React-Redux TypeError:无法读取react-redux中未定义的属性“map” - TypeError: Cannot read property 'map' of undefined in react-redux 类型错误:无法使用 react-redux 读取未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined with react-redux 类型错误:无法读取未定义 React-Redux 的属性“地图” - TypeError: Cannot read property 'map' of undefined React-Redux React-Redux - 类型错误:无法读取未定义的属性(读取“地图”) - React-Redux - TypeError: Cannot read properties of undefined (reading 'map') React-Redux TypeError:无法读取未定义的属性“ setStatus” - React-Redux TypeError: Cannot read property 'setStatus' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux TypeError:无法读取未定义的属性“操作”(React-Redux) - TypeError: Cannot read property 'actions' of undefined (React-Redux) 为什么会出现“ TypeError:无法读取未定义的属性” props”(反应路由器,React-Redux)? - Why am I getting 'TypeError: Cannot read property 'props' of undefined' (react-router, React-Redux)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM