Sazzad Hossain
Sazzad Hossain

Reputation: 57

React Native Firebase database error

I want to develop an app that needs to use firebase database. At first For Test I am trying to develop an app that can read data from firebase database and display data on app. But I found some problem. data not showing. when I debug my code and found the problem is itemsRef is not working.

constructor(props) {
	  super(props);
	  this.state = {
	    dataSource: new ListView.DataSource({
	      rowHasChanged: (row1, row2) => row1 !== row2,
	    })
	  };
	  //this.itemsRef = firebaseApp.database().ref();
	  this.itemsRef = this.getRef().child('items');
	}

	getRef(){
		return firebaseApp.database().ref();
	}

listenForItems(itemsRef) {
  	console.log(itemsRef)
    itemsRef.on('value', (snap) => {
      // get children as an array
      var items = [];
      console.log("kjrnjhe")
      snap.forEach((child) => {
        items.push({
          title: child.val().title,
          _key: child.key
        });
      });
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(items)
      });
    });
  }
  
  componentDidMount() {
  	this.listenForItems(this.itemsRef);
  }

Upvotes: 0

Views: 192

Answers (1)

Mudassir Zakaria
Mudassir Zakaria

Reputation: 437

It looks everything is okay. you can create another project and try to run this code on this new project and also drop this database and create a new one and try again.

Upvotes: 1

Related Questions