0

I'm trying to get info in firebase database, I want the users of one selected store,

"store":{
   "id1":{
     address:"location"
     "users":{
        "id": true
     }
   }
}
"users" : {
  "id":{
    "name": "tom",
    "lastname": "levine",
    "store": {
      "id1": true
    }
  }
}

I have the store id, and I want all the users that match whit that store.

const db = firebase.database();
db.ref('users').child('store').equalsTo('id1').once('value', (snapshot) =>{})

but it is not working, how can I do this? I don't have the userId, just the store that they belong

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
LLaza
  • 369
  • 2
  • 5
  • 13

1 Answers1

0

You've forgotten the orderBy... clause of your query:

db.ref('users').orderByChild('store/id1').once('value', (snapshot) =>{
  snapshot.forEach((user) => {
    console..log(user.key, user.val());
  });
})

But I'm not sure why you're loading them through a query, since you also have an inverted index of all users under each store already. I recommend reading a bit more about creating data that scales in the Firebase documentation and my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Not but I don't want to get all the users, just those that belongs to a specific store. I want this return one or many users if it is the case. – LLaza Jul 30 '17 at 17:43