slava_slava
slava_slava

Reputation: 666

Getting data from firebase not works

I init my web app with firebase, then I add new data into database: {'title': 'hello world!'}. Then I try to get this data: database.ref().child('title') and I got strange object: enter image description here

My code:

export class App extends Component {

    constructor(props) {
        super(props);

        // Initialize Firebase
        var config = {
            apiKey: "&&&&&&&dbBJY_ie3CbXtnL6M6QBEL2JqWI",
            authDomain: "&&&&&&&.firebaseapp.com",
            databaseURL: "&&&&&test-61197.firebaseio.com",
            storageBucket: "test-&&&&.appspot.com",
            messagingSenderId: "7&&&1193299"
        };
        firebase.initializeApp(config);

        // Get a reference to the database service
        var database = firebase.database();

        console.log(database.ref().child('title'));

    }

    login() {
        console.log('login');
        // Log the user in via Twitter

    }

  render() {
    return (
      <View style={styles.container}>
          <Touchable style={styles.loginBtn} onPress={ this.login }>
              <Text style={styles.loginBtnText}>login</Text>
          </Touchable>
      </View>
    );
  }
}

How to get my data? Firebase version: 3.4.0

Upvotes: 0

Views: 221

Answers (3)

Th0rndike
Th0rndike

Reputation: 3436

There are 2 ways of retrieving data in Firebase. First is to wait for an event to happen on your RTDB using a listener:

 var myTitleRef =  database.ref('title');
  myTitleRef.on("value",function(snapshot){
     //snapshot contains the data you're looking for  
     HandleTitle(snapshot);
  });

The other way is to call a REST GET on your data, in this case you need the entire url of the DB and add ".json" at the end. In this example I'll use jQuery:

var myurl = pathToYourDatabase + 'title.json';
$.ajax({
            url: myurl,

            jsonp: "callback",
            dataType: "jsonp",
            success: function( response ) {
                //Response contains data
                DoSomethingWithTitle(response);
            }
        });

For more information, you should read the docs: https://firebase.google.com/docs/web/setup

Upvotes: 2

KpTheConstructor
KpTheConstructor

Reputation: 3291

Your reference is empty and the database is not being called .

Example :

The right way :

var data = database().ref('/your root destination').child('title');

You were missing the () after database .

UPDATE : Your code and the code I've demonstrated above is to actually create a database entry .

Now to get the data from this entry :

data.once('value').then(function(snapshot) {
  var objectData = snapshot.val();

});

Hope this helps

Upvotes: 0

yihangho
yihangho

Reputation: 2215

Firebase queries are asynchronous. Hence, you need to register a callback so that Firebase can inform you when the results are ready.

Try this:

database.ref().child('title').on('value', (snapshot) => {
    console.log(snapshot.val());
});

You may read this for more information.

Upvotes: 2

Related Questions