How do I resolve TypeError: undefined is not an object (evaluating 'rss.items[0].title')

How do I resolve TypeError: undefined is not an object (evaluating 'rss.items[0].title')

大多数时候,它 运行 没有错误,但有时,它根本没有 运行 并给出:我如何解决 TypeError: undefined is not an object (评估 'rss.items[0].title')

这是我的代码,我使用 react-native-rss-parser (https://www.npmjs.com/package/react-native-rss-parser) 来解析我的 rss 提要:

class HomeScreen extends React.Component {
  state = {
    feed: [],
    title0: [],
    title1: []
  };

  componentDidMount() {
    return fetch("https://vnexpress.net/rss/tin-moi-nhat.rss")
      .then(response => response.text())
      .then(responseData => rssParser.parse(responseData))
      .then(rss => {
        this.setState(prevState => ({
          ...prevState,
          feed: rss,
          title0: rss.items[0].title,
          title1: rss.items[1].title
        }));
      });
  }

  render() {
    const Feeds = shuffleArray([
      {
        pic: require("../assets/images/image.jpg"),
        title: Object.keys(this.state.title0).map(k => this.state.title0[k]),
        caption: "caption"
      },
      {
        pic: require("../assets/images/image.jpg"),
        title: Object.keys(this.state.title1).map(k => this.state.title1[k]),
        caption: "caption"
      }
    ]);

    return (
      <SafeAreaView>
        <Swiper
          cards={Feeds}
          renderCard={Card}
          infinite 
          backgroundColor="white"
          cardHorizontalMargin={0}
          stackSize={2} 
        />
      </SafeAreaView>
    );
  }
}

const Card = ({ pic, title, caption }) => (
  <Tile
    imageSrc={pic}
    activeOpacity={1}
    title={title}
    caption={caption}
    featured
    key={title}
  />
);

export default HomeScreen;

更新

Console.log(rss) 结果(确实包含项目):

Object {
  "authors": Array [],
  "categories": Array [],
  "copyright": undefined,
  "description": "VnExpress RSS",
  "image": Object {
    "description": undefined,
    "height": undefined,
    "title": "Tin nhanh VnExpress - Đọc báo, tin tức online 24h",
    "url": "https://s.vnecdn.net/vnexpress/i/v20/logos/vne_logo_rss.png",
    "width": undefined,
  },
  "items": Array [],
  "itunes": Object {
    "authors": Array [],
    "block": undefined,
    "categories": Array [],
    "complete": undefined,
    "explicit": undefined,
    "image": undefined,
    "newFeedUrl": undefined,
    "owner": Object {
      "email": undefined,
      "name": undefined,
    },
    "subtitle": undefined,
    "summary": undefined,
  },
  "language": undefined,
  "lastPublished": "Sat, 30 Nov 2019 21:28:12 +0700",
  "lastUpdated": undefined,
  "links": Array [
    Object {
      "rel": "",
      "url": "https://vnexpress.net/rss/tin-moi-nhat.rss",
    },
  ],
  "title": "Tin mới nhất - VnExpress RSS",
  "type": "rss-v2",
}

您无法访问 rss.items[0].title,因为 rss.items[0] 未定义 在尝试获取标题之前添加一个这样的检查

 componentDidMount() {
    return fetch("https://vnexpress.net/rss/tin-moi-nhat.rss")
      .then(response => response.text())
      .then(responseData => rssParser.parse(responseData))
      .then(rss => {
        this.setState(prevState => ({
          ...prevState,
          feed: rss,
          title0: rss.items[0]? rss.items[0].title:'',
          title1: rss.items[1]? rss.items[1].title:''
        }));
      });
  }