0

I'm trying to get data from firebase within react Native Application.

Attached below is my database: enter image description here

. https://ibb.co/cEVBeb

QWA17SaADRdgluvVnYrXJK1AvI22
-L-uNMmDRnKTkk55KbMz
allowGPS: true  
notificationSound:  true  
phone:  "345345345345345" 
pushNotification:  true 
shareData:  true  
showAlerts:  true 

Below is the code that I've tried and they are not working. Can someone please guide me how can I get the nested values.

firebase.database().ref(`/users/${currentUser.uid}`)
.once('value').then((snapshot) => {
const username = (snapshot.val() && snapshot.val().username) || 'Anonymous';
console.log(snapshot.val().username);
console.log(snapshot.val());
console.log(snapshot.val('username'));
console.log(snapshot.child('phone').val());

2 Answers 2

2

You have a further nest inside if your firebase structure

QWA17SaADRdgluvVnYrXJK1AvI22
    -L-uNMmDRnKTkk55KbMz //HERE

So using snapshot.val() wont access your child components. You should only be creating one of these codes when you make a user, and assigning the key (QWA17Sa...) as its user_id. This other code is uneccessary.

To do this, use .set method when creating users to your firebase, similar to below. This should avoid the creation of this seperate unwanted node in your firebase structure.

    const userPath = `/users/${currentUser}`;

    // Save the text and a url to the saved audio file to firebase.
    firebase.database().ref(userPath).set({
      username: foo
      stuff: foo
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I got to know about this later. Thank you much really helped.
0

Can try this code. For me it works

firebase.database().ref('/users/' + firebase.auth().currentUser.uid).once('value').then(function(snapshot) {
    var json = snapshot.val();
  });

1 Comment

Yeah but it stores the value of an child object. I wanted to get the values of elements inside. Since I was pushing objects in the firebase rather then using .set function, a new child id is created every time. And we will need to loop through the parent to get all elements using the key of child object.

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.