【问题标题】:Firebase Realtime Database shows only last itemFirebase 实时数据库仅显示最后一项
【发布时间】:2019-06-14 16:00:45
【问题描述】:

当我第一次进入此屏幕时,Firebase 会显示每个项目。当我转到另一个屏幕并返回此屏幕时,Firebase 仅显示最后一项。我不知道哪里出了错以及如何解决。

  var data = []
  var firebaseConfig = {
  apiKey: "key",
  authDomain: "information",
  databaseURL: "information",
  projectId: "information",
  storageBucket: "information",
  messagingSenderId: "information",
  appId: "information"
};
firebase.initializeApp(firebaseConfig);
export default class ConfScreen extends React.Component {
 constructor(props){
    super(props);
    this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2})
    this.state = {
      listViewData: data,
      newMarker: "",
    }
  }
  componentDidMount(){
    var that = this
    firebase.database().ref('/markers').on('child_added', function(data){
      var newData = [...that.state.listViewData]
      newData.push(data)
      that.setState({listViewData: newData})
    })
  }
       <List
              enableEmptySections
              dataSource={this.ds.cloneWithRows(this.state.listViewData)}
              renderRow={data => 
                <ListItem>
                  <Text>{data.val().title}</Text>
                </ListItem>
              } />

【问题讨论】:

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


    【解决方案1】:

    我不确定问题出在哪里,但这有更好的工作机会:

    firebase.database().ref('/markers').on('value', function(snapshot){
      var newData = [...that.state.listViewData]
      snapshot.forEach(function(data) {
        newData.push(data);
      });
      that.setState({listViewData: newData})
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多