SuicideSheep
SuicideSheep

Reputation: 5550

Unable to configure firebase in App.js

import React, { Component } from 'react';
import { View, ListView,ToolbarAndroid } from 'react-native';
import ListItem from './components/ListItem';
import styles from './styles';
import * as firebase from 'firebase';

const firebaseConfig = {
  apiKey: 'AIzaSyDEvv0loyjX47GXQp1IlHl3MpR7yCc0KDo',
  authDomain: 'simple-to-do-b592d.firebaseapp.com',
  databaseURL: 'https://simple-to-do-b592d.firebaseio.com',
  projectId: 'simple-to-do-b592d',
  storageBucket: 'simple-to-do-b592d.appspot.com',
  messagingSenderId: '410223452360'
};
// Initialize the firebase app here and pass it to other components as needed. Only initialize on startup.
const firebaseAppx = firebase.initializeApp(firebaseConfig);


export default class App extends Component {
  constructor(props) {
    super(props);
    this.taskRef = firebaseAppx.database().ref();


    const dataSource = new ListView.DataSource({
      rowHasChanged: (row1, row2) => row1 !== row2,
    });

    this.state = {
      dataSource: dataSource
    };
  }

  componentDidMount() {
    // start listening for firebase updates
    this.listenForTasks(this.tasksRef);
  }

  render() {
    return (
      <View style={styles.container}>
      <ToolbarAndroid
        style={styles.navbar}
        title="Todo List" />

        <ListView
          enableEmptySections={true}
          dataSource={this.state.dataSource}
          renderRow={this._renderItem.bind(this)}
          style={styles.listView}/>
      </View>
    );
  }

  _renderItem(task) {
    return (
      <ListItem task={task} />
    );
  }

  listenForTasks(tasksRef) {
    console.log(tasksRef);
    tasksRef.on('value', (dataSnapshot) => {
      var tasks = [];
      dataSnapshot.forEach((child) => {
        tasks.push({
          name: child.val().title,
          _key: child.key
        });
      });

      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(tasks)
      });
});
  }

}

The above code trying to connect to firebase but i keep hitting TypeError: undefined is not an object (evaluating 'tasksRef.on') Tried to console log it but its showing undefined indeed

Upvotes: 0

Views: 34

Answers (1)

Ravi
Ravi

Reputation: 35569

error is here

this.listenForTasks(this.tasksRef);

you need to pass

this.listenForTasks(this.taskRef);

as your local variable name is taskRef not tasksRef

Upvotes: 1

Related Questions