1

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?

1 Answer 1

1

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()));
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.