Here I have a function that is supposed to return either true or false whether or not the document is present in the collection. The query works fine and the result in the console log actually displays the correct boolean value to be returned.
async function isClaimed(voucher, user) {
var db = Firebase.firestore();
var result;
await db
.collection("ClaimedVoucher")
.where("voucherID", "==", voucher)
.where("userID", "==", user)
.get()
.then(function (querySnapshot) {
if (querySnapshot.empty === false) {
result = true;
} else {
result = false;
}
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
console.log(result, "this is result");
return result;
}
However, when I console log what is returned (outside the function), it is the promise, not the boolean value:
isVoucherClaimed = isClaimed(voucher.voucherID, uid);
console.log(isVoucherClaimed);
Displays:
Promise { "_40": 0, "_55": null, "_65": 0, "_72": null, }
I've read that async functions always return promises, however if I remove the async/await then the "result" variable is always undefined, even inside the function. Any help would be greatly appreciated - I am new to react-native / javascript.