0
我想解析來自RSS提要的數據。我現在遇到的錯誤是'TypeError:無法讀取未定義的屬性'標題'(請參閱render()方法)。在React中解析來自RSS feed的對象,爲什麼我的代碼失敗?
getNews()方法似乎正常工作,並將項目對象數組保存爲'news'(請參閱state),因爲我在render方法中檢查了'news'數組的長度是正確的(30)。我究竟做錯了什麼?
>class App extends Component {
constructor(props) {
super(props);
this.state = {
news: []
}
this.getNews = this.getNews.bind(this);
}
componentDidMount() {
this.getNews();
}
getNews() {
$.get("https://www.hs.fi/rss/tuoreimmat.xml", function (data) {
var $xml = $(data);
var items = [];
$xml.find("item").each(function() {
var $this = $(this),
item = {
title: $this.find("title").text(),
link: $this.find("link").text(),
description: $this.find("description").text(),
pubDate: $this.find("pubDate").text(),
}
items.push(item);
}.bind(this));
this.setState({ news: items })
}.bind(this));
}
render() {
var newsItem = this.state.news[1];
var title = newsItem.title
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">News</h1>
</header>
<div className="App-feeds">
</div>
<div className="panel-list">{title}</div>
</div>
);}}
ajax是** asyncnronous **。與試圖在交付之前嘗試吃披薩相同的問題 – charlietfl