组件最初以变量值为null进行安装

时间:2017-07-07 02:52:14

标签: javascript reactjs firebase react-native firebase-realtime-database

此组件之前工作正常。我不确定我做了什么来搞砸这个。但是当组件最初安装时,artist的值为null。然后 - 几乎立即 - 它被分配给我期望的对象。但在此之前,我得到一个错误:

  

无法读取null的属性activePreview

enter image description here

当我解除错误时,数据会在屏幕上正确呈现。所以不确定为什么我会得到这个错误。

enter image description here

为什么这个错误会突然出现?或者为什么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)})
          }
        })
      })
    })
  }

0 个答案:

没有答案