简体   繁体   English

我的 React Js 网站在我的电脑上运行完美,但是当我尝试在我的手机上运行它时,它显示无法读取未定义的属性(读取“地图”)

[英]My React Js website is running perfectly on my pc but when I tried to run it on my phone it shows, Cannot read properties of undefined (Reading 'map')

import React, { Component } from "react";
import Nitems from "./Nitems";
import Loading from "./Loading";
import './Font.css'
import PropTypes from 'prop-types'


export class News extends Component {
  static defaultProps={
    country: "in",
    category: "general"
  }
  static propTypes={
    country: PropTypes.string,
    category: PropTypes.string
  }
  constructor() {
    super();
    console.log("Hii this is a constructor");
    this.state = {
      articles: [],
      loading: false,
      page: 1,
    };
  }
  async componentDidMount() {
    this.props.setProgress(10)
    let url =
    `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=c7908fc25bbc49589fcaedcedfb2e114&pagesize=12`;
    this.setState({loading: true})
    let data = await fetch(url);
    this.props.setProgress(30)
    let parsedata = await data.json();
    this.props.setProgress(70)
    this.setState({
      articles: parsedata.articles,
      totalarticles: parsedata.totalResults,
      loading: false
    });
    this.props.setProgress(100)
  }
  previouspage = async () => {
    this.props.setProgress(10)
    let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}$apiKey=c7908fc25bbc49589fcaedcedfb2e114&page=${
      this.state.page - 1
    }&pagesize=12`;
    this.setState({loading: true})
    let data = await fetch(url);
    let parsedata = await data.json();
    console.log(parsedata);
    this.setState({
      page: this.state.page - 1,
      articles: parsedata.articles,
      loading: false
    });
    this.props.setProgress(100)
  };
  
  nextpage = async () => {
    this.props.setProgress(10)
    if (this.state.page + 1 > Math.ceil(this.state.totalarticles / 12)) {
      
    } 
    else {
      let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=c7908fc25bbc49589fcaedcedfb2e114&page=${
        this.state.page + 1
      }&pagesize=12`;
      
      this.setState({loading: true})
      let data = await fetch(url);
      let parsedata = await data.json();
      console.log(parsedata);
      this.setState({
        page: this.state.page + 1,
        articles: parsedata.articles,
        loading: false
      });
      this.props.setProgress(100)
    }
  };
  render() {
    return (
      <div className="container ">
        <h3 className="my-3" className="text-center" style={{margin: "18px 0px", fontFamily:" Permanent Marker", fontSize: "45px"}}>Top Headlines 💥</h3>
        {this.state.loading && <Loading/>}
        <div className="row">
          {!this.state.loading && this.state.articles.map((element) => {
            return (
              <div className="col-md-4" key={element.url}>
                <Nitems
                  title={element.title.slice(0, 50)}
                  description={
                    element.description
                      ? element.description.slice(0, 45)
                      : "Description is not available. The ADP"
                  }
                  img={element.urlToImage}
                  nurl={element.url}
                  date={element.publishedAt}
                  author={element.source.name}
                ></Nitems>
              </div>
            );
          })}
        </div>
        <div class="d-flex justify-content-between">
          <button
            disabled={this.state.page <= 1}
            type="button"
            class="btn btn-danger"
            onClick={this.previouspage}
          >
            👈 Previous
          </button>
          <button disabled={(this.state.page + 1 > Math.ceil(this.state.totalarticles / 12))} type="button" class="btn btn-danger" onClick={this.nextpage}>
            Next 👉
          </button>
        </div>
      </div>
    );
  }
}

export default News;

My React Js website is running perfectly on my pc but when I tried to run it on my phone it shows, Cannot read properties of undefined (Reading 'map').我的 React Js 网站在我的电脑上运行完美,但当我尝试在我的手机上运行它时,它显示无法读取未定义的属性(读取“地图”)。

If i got the problem, then this is causing the trouble;如果我遇到了问题,那么这就是问题所在; Inside your render method;在你的render方法中;

{!this.state.loading && this.state.articles.map((element) 

As you are fetching articles from some url, it takes some time so, first, this.state.articles is undefined.当您从一些 url 获取文章时,需要一些时间,因此,首先, this.state.articles是未定义的。

So try this;所以试试这个;

{!this.state.loading && this.state.articles && this.state.articles.map((element)

Try to add the ?尝试添加? after articles and then try it.看完文章再试试吧。 after adding this you will not get the cannot read property of undefined error.添加此后,您将不会获得cannot read property of undefined And you will able to debug you app.你将能够调试你的应用程序。

 {!this.state.loading && this.state.articles?.map((element) => {
        return (
          <div className="col-md-4" key={element.url}>
            <Nitems
              title={element.title.slice(0, 50)}
              description={
                element.description
                  ? element.description.slice(0, 45)
                  : "Description is not available. The ADP"
              }
              img={element.urlToImage}
              nurl={element.url}
              date={element.publishedAt}
              author={element.source.name}
            ></Nitems>
          </div>
        );
      })}

暂无
暂无

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

相关问题 当我在我的 React 应用程序中使用 map 方法时,出现此错误“无法读取未定义的属性(读取‘地图’)” - I am getting this error "Cannot read properties of undefined (reading 'map')" when i use map method in my react app 我的 map 给了我无法读取未定义的属性(读取“地图”) - My map gives me Cannot read properties of undefined (reading 'map') 无法读取未定义的属性(读取“地图”)我不能再次使用我的变量 - Cannot read properties of undefined (reading 'map') I can't use my variable again 无法读取未定义的属性(读取“地图”)反应 JS - Cannot read properties of undefined (reading 'map') react JS TypeError:无法读取未定义的属性(读取“地图”)React JS - TypeError: Cannot read properties of undefined (reading 'map') React JS TypeError:无法在反应 JS 中读取未定义的属性(读取“地图”) - TypeError: Cannot read properties of undefined (reading 'map') in react JS 无法读取未定义的属性(读取“搜索”)。 我的位置返回未定义,这是 react-router-dom v6 - Cannot read properties of undefined (reading 'search'). My location returning undefined and this is react-router-dom v6 我该如何解决这个问题? 类型错误:无法读取 react.js 中未定义(读取“地图”)的属性 - How can i solve this issue ? TypeError: Cannot read properties of undefined (reading 'map') in react.js 我收到错误:运行我的反应项目时无法读取未定义的属性'then' - I get the error : Cannot read property 'then' of undefined when I run my react project 函数在我的 redux 状态可用之前运行,TypeError:无法读取未定义的属性(读取“查找”) - function is running before my redux state is available, TypeError: Cannot read properties of undefined (reading 'find')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM