I am trying to pull a user's information from Firestore by querying the 'uid' field of the 'users' collection. Which works fine.
The problem is when there is no user with a matching uid, I would like the function to return false or null so that I can use this function in an if statement to create a user profile
but querySnapshot always returns an object containing meta data even if the query is not successful.
So How can I handle this?
async function getUserData (uid) {
const usersRef = collection(db, "users");
const q = query(usersRef, where("uid", "==", uid));
const querySnapshot = await getDocs(q);
console.log(querySnapshot);
return (querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
if(doc) {
return doc.data();
} else {
console.log('else');
return false;
}
}));
};