简体   繁体   English

我正在读取未定义的属性 map。我已经尝试了一切但没有任何效果

[英]I am getting read property map of undefined.I have tried everything but nothing worked

I have tried many solutions given on StackOverflow as well as on other platforms but nothing worked.我在 StackOverflow 以及其他平台上尝试了许多解决方案,但没有任何效果。 I am new to ReactJs and I am unable to understand what is wrong in this code that I am getting this error.我是 ReactJs 的新手,我无法理解我收到此错误的代码有什么问题。

componentDidMount() {
console.log("component did mount");
axios.get('http://localhost:3000/')
.then(response => {
  if (response.data.length > 0) {
    this.setState({ 
      blogs: response.data
    });
  }
})
.catch((error) => {
  console.log(error);
})

}

render(){
console.log("inside render ");

  var b=this.state.blogs.map((blog)=>{
    console.log(blog);
     var taggu=blog.tag.map((tag)=>{
      return(
        <span>{tag}</span>
      )
      });
    var con=blog.content.slice(0,100);
    return(
      <Col className="my-1" lg="4" sm="6" >
      <Card key={blog._id}>
          <CardHeader tag="h3">{blog.topic}</CardHeader>
          <CardBody>
            <CardTitle tag="h5">By {blog.writer.username}</CardTitle>
            <CardText>{con}... </CardText>
            <Button>Learn More</Button>
          </CardBody>
          <CardFooter>{taggu}</CardFooter>
        </Card>
      </Col>
     )
  });


return ( <div className="App">{b}</div>)

} }

The Promise from axios does not resolve instantly and it is very likely that the render function was called before来自Promiseaxios不会立即resolve ,很可能之前调用了render function

this.setState({ 
  blogs: response.data
});

was executed, meaning that this.state.blogs is undefined.已执行,这意味着this.state.blogs未定义。

You could either add您可以添加

this.setState({ 
  blogs: []
});

in the constructor or check whether this.state.blogs is undefined before calling its map function:在构造函数中或在调用其 map function 之前检查this.state.blogs是否未定义:

render(){
  console.log("inside render ");
  if(!this.state.blogs)
    return <span>Loading...</span>;
  // Rest of the code below...

暂无
暂无

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

相关问题 我正在尝试从数据库中获取 map mydata 但我收到此错误。 TypeError:无法读取未定义的属性“地图” - I am trying to map mydata from the database but i am getting this error. TypeError: Cannot read property 'map' of undefined 为什么我收到错误:类型错误:无法读取未定义的属性“地图”? - Why am I getting an error : TypeError: Cannot read property 'map' of undefined? 尝试访问嵌入的Google地图时,为什么会出现无法读取未定义的属性“ getCenter”的问题? - Why am I getting Cannot read property 'getCenter' of undefined when trying to access my embed google map? 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 我收到错误 Typerror 无法读取未定义的属性“执行” - I am getting an error Typerror Cannot read property 'execute' of undefined 我在控制台中收到此错误:无法读取未定义的属性“ some” - I am getting this error in the console : Cannot read property 'some' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 我收到错误“无法读取未定义的属性“用户名”” - I am getting the error "cannot read property "username" of undefined"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM