Suman Shaw
Suman Shaw

Reputation: 143

How do I fetch data from firebase realtime database which are children to a random key - using react native?

I am new to react native and I am using the Firebase Realtime database. I am trying to fetch all the 'sleep' values from the firebase db - as shown in this screenshot.

enter image description here

I could proceed till here -

constructor(props) {
  super(props);
var sleep = db.ref('symptoms/');
sleep.on('value', function(snapshot) {
  console.log(snapshot.val());
});
}

and fetch all the data in the symptoms tree as so - enter image description here

But I am only interested in fetching the sleep values which are inside these random keys. I don't know how to navigate through the keys - specially when new keys are generated when a new child gets added - and I want to be able to fetch the newly added child as well.

Does anyone know how to do this ?

Upvotes: 1

Views: 220

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80934

You need to iterate to retrieve the sleep value:

constructor(props) {
  super(props);
var sleep = db.ref('symptoms/');
sleep.on('value', function(snapshot) {
  snapshot.forEach((childSnapshot) => {
  console.log(childSnapshot.val().sleep);
  });
});
}

Upvotes: 1

Related Questions