简体   繁体   English

未捕获的类型错误:无法读取未定义的属性(读取“长度”)

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'length')

I am developing a react app, but i am facing this error-> "Uncaught TypeError: Cannot read properties of undefined (reading 'length')".我正在开发一个 React 应用程序,但我遇到了这个错误->“未捕获的类型错误:无法读取未定义的属性(读取‘长度’)”。 how to fix this?如何解决这个问题? my code is:我的代码是:

import React from "react";
import { Grid, CircularProgress } from "@material-ui/core";
import Post from "./Post/Post";
import useStyles from "./styles";
import { useSelector } from "react-redux";

const Posts = () => {
  const posts = useSelector((state) => state.Posts);
  const classes = useStyles();

  console.log(posts);
  return (!posts.length ? <CircularProgress/> :(
   <Grid className={classes.container} container alignItems="stretch" spacing={3}>
      {
        posts.map((post)=> (
              <Grid key={post._id} item xs={12} sm={6}>
                <Post post = {post} />
              </Grid>
        ))
      }

   </Grid>
 )
  );
};

export default Posts;

use .post || !post.length使用.post || !post.length .post || !post.length or ?posts..length .post || !post.length?posts..length

You can try something like this, ?posts.?length > -1 ?你可以尝试这样的事情, ?posts.?length > -1 ? . . As you are checking if the post array is empty or not.当您检查 post 数组是否为空时。

try posts && <Grid>试试posts && <Grid>

The error says the type of posts is Undefined.该错误表明帖子的类型是未定义的。 As you are getting that value from Posts in state object, that means that Posts is not initialized in your state. Try initializing "Posts" with empty Array ([]) in your initial state object.当您从 state object 中的Posts获取该值时,这意味着Posts未在您的 state 中初始化。尝试在初始 state object 中使用空数组([])初始化“Posts”。

暂无
暂无

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

相关问题 未捕获的类型错误:无法在 GoogleMapReact 读取未定义的属性(读取“长度”) - Uncaught TypeError: Cannot read properties of undefined (reading 'length') at GoogleMapReact 未捕获的类型错误:无法读取未定义的属性(读取“长度”) - Uncaught TypeError: Cannot read properties of undefined (reading 'length') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 类型错误:无法读取未定义的属性(读取“长度”) - TypeError: Cannot read properties of undefined (reading 'length') 类型错误:无法读取未定义的属性(读取“长度”) - TypeError: Cannot read properties of undefined (reading 'length') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM