'Helper firebase.firestore query function keeps returning undefined even though I want it to return a boolean
My over all goal is to see if a user has made a session in the past on the site. I want to make a helper function that returns a boolean depending on if a user has created a session or not. I am using firebase.firestore() and firebase.auth() for this project. My plan is to query firestore and get an array of emails that are correspond to all the sessions. Then I will loop through all those emails. If one matches the firebase.auth().currentUser.email then I will return true. If it doesnt match then I will return false.
The function keeps returning undefined. I thought it was because you cannot have a break in a forEach loop which is what I was using before.
How to break out of the querySnapshot forEach loop method?
After I read this post I converted my forEach loop into a regular for loop. This did not fix the problem and it keeps returning undefined instead of a boolean.
// inputs in a user email
// outputs a boolean
// (t if they have made a session)
// (f if they haven't made a session)
function madeSessionHelper (userMail) {
let res;
firebase.firestore().collection('sessions')
.get()
.then((querySnapshot) => {
for (let i in querySnapshot.docs) {
const doc = querySnapshot.docs[i]
if (doc.data().Goal_Email === userMail) {
res = true;
break;
} else {
res = false;
break;
}
}
return res;
})
}
console.log(madeSessionHelper("[email protected]"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
