此组件之前工作正常。我不确定我做了什么来搞砸这个。但是当组件最初安装时,artist
的值为null。然后 - 几乎立即 - 它被分配给我期望的对象。但在此之前,我得到一个错误:
无法读取null的属性activePreview
当我解除错误时,数据会在屏幕上正确呈现。所以不确定为什么我会得到这个错误。
为什么这个错误会突然出现?或者为什么artist
最初是null
?
componentDidMount = () => {
// Get the uids of all artists user follows
db.ref(`users/${this.props.uid}/following`).once('value', (snapshot) => {
snapshot.forEach((childSnapshot, index) => {
var id = childSnapshot.val().uid;
// For each of those ids, check if the artist has an active preview
db.ref(`users/${id}`).once('value', (snapshot) => {
const artist = snapshot.val();
console.log(artist);
// If they do, add them to the activePreviews array
if (artist.activePreview) {
this.setState({activePreviews: this.state.activePreviews.concat(artist)})
}
})
})
})
}