0

I have a list of place objects stored in Firebase Realtime Database like this:

database setup

And I'd like to filter and retrieve a snapshot of a group of places by their IDs as so: ["ChIJ16pHtWSfwokRhw2T-Kt5EIM", "ChIJ4UHbQuihwokRUj0afTh7s6g", ChIJAV0IY-ihwokRtf2PC4Cf9W0]

My current method involves looping and retrieving a snapshot of each individually. The reason it makes me uncomfortable is because this would require a nested snapshot reading. Which I am unsure is good practice or not. The first snapshot is to retrieve the list of ID's. Is there a more efficient way?

        for (place in section) {
            var place_data = target.child(place)
            place_data.once('value').then(snapshot => {
                console.log("FOUND SNAPSHOT")
                console.log(snapshot.val())
                if (snapshot.val()) {
                    dict[key].append(snapshot.val())
                    return snapshot.val()
                } else { return null }
            })
            .catch((err) => {
                        console.log(err);
            });

        }
degenPenguin
  • 725
  • 1
  • 8
  • 23
  • Thanks but I'm using Realtime Database, not using Firestore – degenPenguin Aug 03 '19 at 15:47
  • 1
    You're asking about an 'IN' type query, which is where you retrieve all of the nodes that match what's in your list. Firebase does not support that directly. You'll either need to iterate over the ones you want and read them in one at a time, or find a common element for each node so you can do a query or lastly, denormalize your data where the groups are associated by some other child. There are lot of [similar questions](https://stackoverflow.com/search?q=%5Bfirebase%5DIN+query) here on SO so check those out for some ideas. – Jay Aug 03 '19 at 16:01
  • 1
    You might want to check this [other thread](https://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786) – Morphyish Aug 03 '19 at 16:03

0 Answers0