Umer Afzal
Umer Afzal

Reputation: 11

How can I retrieve data from firebase realtime database?

I want to show data from Firebase realtime database into my app. I have integrated it with firebase database and can use update, delete, create user in db.

My database structure is like this

schooldbproject
-class_1
  1
    age: 24
    name:"Pheng Sengvuthy 004"

This is the code I wrote to create db:

 // To select data from firebase every time data has changed !
 firebase.database().ref('schooldbproject').on('value', (data) => {
        console.log(data.toJSON());
    })


 //create
 firebase.database().ref('class_1/1').set(
            {
                name: 'Pheng Sengvuthy 004',
                age: 24
            }
        ).then(() => {
            console.log('INSERTED !');
        }).catch((error) => {
            console.log(error);
        });


 // To Update a user
 firebase.database().ref('users/004').update({
        name: 'Pheng Sengvuthy'
    });

How can I show data into text component as I am creating school CMS?

Upvotes: 1

Views: 186

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599996

You seem to be repeating the database name here:

firebase.database().ref('schooldbproject').on('value', (data) => {
    console.log(JSON.stringify(data.val()));
})

That is not needed. The path you read from should be the same as the path you write to

firebase.database().ref('class_1/1').on('value', (data) => {
    console.log(JSON.stringify(data.val()));
})

If you want to read the entire database, request a reference to the root by passing no arguments to ref():

firebase.database().ref().on('value', (data) => {
    console.log(JSON.stringify(data.val()));
})

Upvotes: 1

Related Questions