0

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

Blue Bot
  • 2,278
  • 5
  • 23
  • 33

1 Answers1

1

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.

Khauri
  • 3,753
  • 1
  • 11
  • 19
  • 1
    So by using promises with Firebase connection Im not sending a `http` request for each query? – Blue Bot Mar 25 '18 at 19:28
  • 1
    No, I don't think so. Firebase doesn't use http requests but rather Websockets/long polling most likely. While http opens a connection, sends a request then closes it, firebase opens the connection then keeps it open. – Khauri Mar 25 '18 at 21:46