Within an API library function, I am utilizing firestore
to fetch data. This aspect of the code is functioning correctly:
export const getUserByReferrerId = async id => {
let doc = await firestore
.collection(FIRESTORE_COLLECTIONS.USERS)
.where('grsfId', '==', id)
.get()
.then(querySnapshot => {
if (!querySnapshot.empty) {
console.log("we found a doc");
// accessing only the first document, though there might be more
const snapshot = querySnapshot.docs[0];
console.log("snapshot", snapshot.id);
return snapshot.id; // extracting user's uid
}
});
}
From within a component, I am invoking this library. There is another function triggered by a button click, and I am struggling with capturing the value from the async
api call.
I attempted this approach - observing a promise when I console.log
the outcome:
testCreditFunction = (id) => {
let uid = getUserByReferrerId(id).then(doc => {
console.log("uid1", doc);
});
}
Additionally, I tried this method - with the log indicating null for uid
.
testCreditFunction = (id) => {
let uid = '';
(async () => {
uid = await getUserByReferrerId(id);
console.log("uid in the function", uid);
})();
}
I have noticed similar queries raised multiple times, and despite experimenting with various solutions, none seem to work in my case. It's puzzling as I've successfully implemented the same process elsewhere, leaving me uncertain of what sets this instance apart.