scienst
scienst

Reputation: 103

Can't retrieve values for a specific child with firebase

I am using firebase and I want to only retrieve childs with a specific value (activiteId) for a given key ("associationId")

What I tried:

firebase.database().ref().child("collectivites").child(collectiviteId).child("evenements").orderByChild("associationId").equalTo(activiteId).once("value", function (snapshotAssociationEvenements) {
  const data = snapshotAssociationEvenements.val();
  if (data){
   console.log(data);
  }
});

The problem : The child is displayed in the console but if I do console.log(snapshotAssociationEvenements.val().associationId); the console returns undefined

What have I done wrong ?

My db enter image description here

Thank you !

Upvotes: 1

Views: 48

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Try the following:

firebase.database().ref().child("collectivites").child(collectiviteId).child("evenements").orderByChild("associationId").equalTo(activiteId).once("value", function (snapshotAssociationEvenements) {
snapshotAssociationEvenements.forEach(function(childSnapshot) {
   var associateId=childSnapshot.val().associationId;
});

Your snapshot is at the child evenements, then you loop inside the random id using the forEach and you will be able to access the children under that id.

Upvotes: 2

Related Questions