Home > database >  Firebase query returns undefined
Firebase query returns undefined

Time:07-01

I was trying to make a query to get data from my database (Firebase), I was trying to fetch all the information from the array, inside the array there are only user ids, according to these ids, I am fetching the information of the users, the problem is that it returns "undefined" in each query that I do, this is my code:

const loadInformation = async () => {
        const data = await db.collection('channels').doc(id).get();
        setInformation(data.data());
        const users = await Promise.all(
            data.data().users.map((user) => {
                db.collection('user').doc(user).get();
                /* db.collection('user').where("userId", "==", user).onSnapshot(snap => {
                    setAllUsers(snap.docs.map(doc => ({
                        id: doc.id,
                        data: doc.data()
                    })))
                }) */
            })
        )
        setAllUsers(users)
    }

In the first line I am getting the data from the channel and then I am trying to get the data from the users according to the result of the first request.

On the first request, it works fine, and in the loop (map), the user ids work normally (I did a console.log(), and it works).

this is the model of my database:

enter image description here

CodePudding user response:

Since you're not returning anything from the innermost closure, you end up with an empty array in your call to Promise.all(). Putting return at the start of the line with the get() call should fix that.

  • Related