Blue Bot
Blue Bot

Reputation: 2438

Is it possible to use a dynamic query in Firebase?

My aim is to get users that have id listed in an array.

EXAMPLE: I have an array of id's

const usersId = [222, 444, 555, 522]

and I want to get all users with those id's. each id is unique, meaning there can be only one user with that id.

I looked in the docs and from what I understood I need to handle this programmatically. So my only choice is to make a get request for each id? this can be problematic because they can be more then a couple hundreds

this is my helper func:

export const getById = (id) => {

    db.collection('users')
        .where('id', '==', id)
        .get()
        .then(querySnapshot => {
            querySnapshot.forEach(doc => {
                console.log(doc.id, " => ", doc.data());
            });
        })
}

and this is a simplified example for the loop:

usersId.forEach(id => {
    getById(id);
})  

Is there a better way to handle this?

Thanks

Upvotes: 0

Views: 375

Answers (1)

Khauri
Khauri

Reputation: 3863

this can be problematic because they can be more then a couple hundreds

Actually as far as I understand it's not problematic. It's unintuitive, but by design you're supposed to make a bunch of requests like that because Firebase actually pipelines all the requests, which makes it faster than you'd think.

If you use something like Promise.all you should be able to better streamline your approach as well.

Upvotes: 1

Related Questions