Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

javascript - 如何解决TypeError:undefined不是对象(评估'rss.items [0] .title')(How do I resolve TypeError: undefined is not an object (evaluating 'rss.items[0].title'))

Most of the time, it runs with no error, but sometimes, it doesn't run at all and gives out: How do I resolve TypeError: undefined is not an object (evaluating 'rss.items[0].title')(在大多数情况下,它可以正常运行,但有时却根本不运行,并给出以下信息:我如何解决TypeError:undefined不是对象(评估'rss.items [0] .title'))

This is my code, I use react-native-rss-parser ( https://www.npmjs.com/package/react-native-rss-parser ) to parse my rss feed:(这是我的代码,我使用react-native-rss-parserhttps://www.npmjs.com/package/react-native-rss-parser )解析我的rss feed:) 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; UPDATE(更新) Console.log(rss) result (It does contain items):(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", }   ask by DarkLord666 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you cant acsess rss.items[0].title as rss.items[0] is undefined just add a check like that before trying to get the title(您无法访问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:'' })); }); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...