I need to get back an array of documents using an array of user Ids from firestore in react. I want to call this as a function and pass in an array of ids into it, then return an array of objects with all the documents that have matching userIds.
The userIds array will look something like this
["2","3","4"]
The object I need returned in an array, and how it is structured as a collection in the database will look like this:
{
dateCreated: 1630348731918,
docId: "ljAIJmthfwSLwU4FPdMW",
emailAddress: "[email protected]",
followers: ["2","3"],
following: ["2"],
fullName: "Bobby R.",
userId: "3",
username: "bobby"
}
Below is the code I currently have.
export const getFollowers = async (followerIds) => {
if (followerIds.length > 0) {
const result = await firebase
.firestore()
.collection("users")
.where("userId", "in", followerIds)
.get();
const followers = result.docs.map((item) => ({
...item.data(),
docId: item.id,
}));
return followers;
}
};
I'm calling this inside useEffect, which looks like this in the full page:
export default function FollowerPopUp({ visible, closeWindow, followers }) {
useEffect(() => {
const response = getFollowers(followers);
console.log(response);
}, [followers]);
return (
<div
className={`overflow-auto z-30 m-0 p-0 h-2/6 w-3/12 border rounded-xl bg-white text-left fixed ${visible}`}
>
<div className="flex border-b border-gray-primary w-full h-8 mt-2 px-5 ">
<p className="mx-auto font-bold pl-6">Followers</p>
<img
src="/images/cancel.png"
className="cursor-pointer h-5 mt-0.5"
onClick={closeWindow}
/>
</div>
<FollowerRow
following={true}
username={"kratos"}
fullName={"Kratos God of War"}
/>
</div>
);
}
And I'm actually now getting back a promise with the Promise Result and the array of objects I need. Now I just need to get that array into a variable to map through.
Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Array(2)
0: {username: "kratos", following: Array(0), userId: "2", fullName: "Kratos the God of War", followers: Array(1), …}
1: {dateCreated: 1630347676459, userId: "3", following: Array(1), emailAddress: "[email protected]", username: "cloud", …}
length: 2
followerIds, nor the value returned, nor can we see the contents of your dataabase Please edit the question to provide your debugging details and your minimal complete example that anyone can use the reproduce the issue.