1
votes

My data in firebase looks as shown below.

items {
   item1: {
      field1: value1
      field2: value2
   }
   item2: {
      field1: value4
      field2: value3
   }
}

Now, I want to get all items where field2===value3. When I run the following query, I'm getting all items. Why?

firebase.database().ref().child('items')
        .orderByChild('field2')
        .equalTo('value3')
        .limitToLast(10)
        .once('value');
1
Are you using the Firebase version 3 API or the old version 2 API? The code doesn't look right. Where is the ref? With the version 3 API, it would be something like firebase.database().ref().child(... - cartant
I'm using the latest. In the code, firebase is my local variable which is qual to firebase.database().ref(). Let me change the question's code for better clarity - user43286
You say you get 'all items'. How many do you get? Can you include the received items in your question? - cartant
@cartant Sorry,ignore this. I was stupid. There was a parallel thread which was setting different result without filter. - user43286

1 Answers

0
votes

Instead of using queries, you can use child_added listener which will return all subitems in items and then you can perform the actions you want.

firebase.database().ref('items').on('child_added', snap => {
    if(snap.val().field2 == 'value3') {
        console.log('Match found in ' + snap.key); //Match found in item2
    }
});