I want to get my data with two nested database query after taking the whole daha with this process. I want to set it into my useState but useState always set []
. I know that this is about async process but how can I fix it?
useEffect(() => {
const subscriber = firebase
.firestore()
.collection("Users")
.doc(firebase.auth().currentUser.uid)
.onSnapshot((querySnapshot) => {
const followers = [];
querySnapshot.data()["followers"].forEach(async (data) => {
const fuser = await firebase
.firestore()
.collection("Users")
.doc(data["uid"])
.get();
followers.push({
name: fuser.data()["name"],
nickname: fuser.data()["nickname"],
surname: fuser.data()["surname"],
profilePhotoURL: fuser.data()["profilePhotoURL"],
key: fuser.id,
});
console.log(followers, "1"); // and second execute that
});
setFollowers(followers); // It always set []
console.log(followers, "2"); //First execute that
setLoading(false);
});