2017-04-08 45 views
0

我試圖在我的firebase查詢中使用asyncStorage中的值。使用異步存儲的Firebase查詢

我對此感到困惑。我嘗試了異步/等待,然後嘗試/捕獲。

不知道我該怎麼做。

constructor(props) { 
    super(props); 
    this.state = { 
     dataSource: new ListView.DataSource({ 
     rowHasChanged: (row1, row2) => row1 !== row2, 
     }), 
    }; 

    const user = firebase.auth().currentUser; 
    if (user != null) { 
     AsyncStorage.getItem('parentUid') 
     .then((data) => { 
      this.setState({ pUID: data }); 
     }); 
     this.itemsRef = this.getRef().child(`Stores/${this.state.pUID}/`); 
    } else { 
     Alert.alert('Error', 'login again!'); 
    } 
    this.connectedRef = firebase.database().ref('.info/connected'); 
    } 

回答

0

我已經從構造函數移動到反應生命週期,它工作正常。如果有人需要,這是代碼。

componentWillMount() { 
    AsyncStorage.getItem('parentUid') 
     .then((data) => { 
     this.setState({ pUID: data },() => { 
      this.itemsRef = this.getRef().child(`Stores/${this.state.pUID}/`); 
     }); 
     }) 
     .then(() => { 
     this.connectedRef.on('value', this.handleValue); 
     }); 
    }