2017-08-07 67 views
0

我是新來的react-native,並試圖用firebase構建一個簡單的應用作爲後端。反應本地獲取實時數據庫數據不工作(firebase)?

我已經進行了註冊,登錄,註銷並設置了一個移動號碼。我正在嘗試實時讀取移動號碼數據。獲取數據的功能如下:

_listenForMobileNumberChanges(userId, callback) { 

    let userMobilePath = "/user/" + userId + "/details"; 

    firebase.database().ref(userMobilePath).on('value', (snapshot) => { 

     var mobile = ""; 

     if (snapshot.val()) { 
      mobile = snapshot.val().mobile 
     } 

     callback(mobile) 
     }); 
    } 

在渲染功能,我把一個ListView和調用的函數:

<ListView 
     dataSource={this.state.mobile} 
     renderRow={() => {this._listenForMobileNumberChanges(this.state.userId}} 
    /> 

我知道我錯過了在ListView調用回調函數參數,但我不知道要添加到參數中,有誰能幫忙嗎?

回答

0

而不是使用一個ListView使用的TextInput,而不是在檢測TextInput的onChangeText屬性作出這樣的metode打電話:

<TextInput 
     style={{height: 40, borderColor: 'gray', borderWidth: 1}} 
     onChangeText={(usermobileNumber) => this._listenForMobileNumberChanges()} 
     value={this.state.usermobileNumber} 
    /> 

解析無參數只是修改了Methode這樣:

_listenForMobileNumberChanges() { 
    alert('inside the listen function'); 

    if(firebase.auth().currentUser) { 
     let userId = firebase.auth().currentUser.uid; 
     let userMobilePath = "/user/" + userId + "/details"; 

     firebase.database().ref(userMobilePath).on('value', (snapshot) => { 

      var mobile = ""; 

      if (snapshot.val()) { 
       mobile = snapshot.val().mobile 
      } 

      this.setState ({usermobileNumber: mobile}); 
     }); 
    } else { 
     this.setState ({usermobileNumber: ''}); 
    } 
    }