New to Firebase and JS and have been stuck on this for a long time (really thankful for any help)
Fetch data from a Firestore collection 'users' from a document named after the uid of the current user. From that document, I want to save the value of a 'company' field to a variable, to later filter using that value.
I get 'undefined' when calling the getUserCompany function from the Dashboard function.
I think the issue might be that because the getUserCompany function is asynchronous, I cannot call it from a non-async function (or at least get the value of the Promise)
What should I do to produce the intended outcome? (might require a completely different approach)
NB: projectFirestore is my firebase.firestore()
function getUserCompany() {
const user = projectAuth.currentUser;
if (user !== null) {
const userUID = user.uid // correct userUID (works to this point)
projectFirestore.collection('users').doc(userUID).get().then((snapshot) => {
const usercompany = snapshot.data().company
console.log(usercompany) // Logs correctly = company of the current user
return usercompany
})
}}
export default function Dashboard() {
const value = getUserCompany()
console.log(value) // logs 'undefined'
/* ...Later used to filter */
}