【发布时间】:2016-04-07 06:07:09
【问题描述】:
在componentDidMount中,第一个console.log(this)给了我正确的响应("ThumbNail {props: Object, context: Object, refs: Object, updater: Object, state: Object...}")
但在 firebase 查询(即 orderbyChild)中使用时,它的 console.log(this) 返回 null。我正在尝试在查询中使用函数“this.handleArticle”,但我不断收到此错误“无法读取属性 '_handleArticle' of null”。
componentDidMount() {
//adds the change listener to listen to changes in TeamStore
TeamStore.addChangeListener(this._onChange);
// triggers updateArticle
// bind article to current state.name
this._updateArticle();
console.log(this); //this one is right
//get new article as notification
var teamResRef = new Firebase(this.props.baseUrl + this.state.name + '/results');
teamResRef.orderByChild('timeStamp').startAt(Date.now()).on('child_added', function(snapshot) {
var newArticle = snapshot.val();
// console.log(newArticle);
// _handleArticle(newArticle);
console.log(this); //this one returns null
});
}
【问题讨论】: