I am having a hard time returning a value from the firestore database. I am trying to return the 'amount' from the database. When setting the variable i am able to console.log the 'amount'. (see code) But when i try to return the value at the end of the function, it does not return anything.('amount' is not defined no-undef) How can i return this value. Any help would be great. Please keep in mind that i am still quite new to this topic.
import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
export default function checkAmount() {
let user = firebase.auth().currentUser;
if (user) {
let userUID = user.uid
let docRef = firebase.firestore().collection("users").doc(userUID);
docRef.get().then((doc) => {
if (doc.exists) {
let amount = doc.data().amount;
if (amount > 0){
console.log(amount) /// This does work
}
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
}
return amount /// This **does not** return anything . How do i return the amount?
}
I am having a hard time returning a value from the firestore database. I am trying to return the 'amount' from the database. When setting the variable i am able to console.log the 'amount'. (see code) But when i try to return the value at the end of the function, it does not return anything.('amount' is not defined no-undef) How can i return this value. Any help would be great. Please keep in mind that i am still quite new to this topic.
import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
export default function checkAmount() {
let user = firebase.auth().currentUser;
if (user) {
let userUID = user.uid
let docRef = firebase.firestore().collection("users").doc(userUID);
docRef.get().then((doc) => {
if (doc.exists) {
let amount = doc.data().amount;
if (amount > 0){
console.log(amount) /// This does work
}
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
}
return amount /// This **does not** return anything . How do i return the amount?
}
Share
Improve this question
edited Mar 27, 2023 at 21:08
Renaud Tarnec
83.2k10 gold badges98 silver badges129 bronze badges
Recognized by Google Cloud Collective
asked Dec 30, 2019 at 21:54
SanMiguelSanMiguel
451 silver badge8 bronze badges
1
- could you share the structure in datastore please? – Soni Sol Commented Dec 31, 2019 at 1:14
1 Answer
Reset to default 7The reason is because the get()
method is asynchronous: it returns immediately with a promise that resolves some time later with the results of the query. The get()
method does not block the function (it returns immediately, as said above): this is why the last line (return amount
) is executed before the asynchronous work is finished but with an undefined value.
You can read more here on asynchronous JavaScript methods and here on why Firebase APIs are asynchronous.
You therefore need to wait that the promise returned by the get()
resolves and use the then()
method, as Alex mentioned, to receive the query results and send the response.
The following will work:
export default function checkAmount() {
let user = firebase.auth().currentUser;
if (user) {
let userUID = user.uid
let docRef = firebase.firestore().collection("users").doc(userUID);
return docRef.get().then((doc) => { //Note the return here
if (doc.exists) {
let amount = doc.data().amount;
if (amount > 0){
console.log(amount) /// This does work
return true; //Note the return here
}
} else {
console.log("No such document!");
//Handle this situation the way you want! E.g. return false or throw an error
return false;
}
}).catch(error => {
console.log("Error getting document:", error);
//Handle this situation the way you want
});
} else {
//Handle this situation the way you want
}
}
But you need to note that your function is now also asynchronous. Therefore you should call it as follows:
checkAmount().
then(result => {
//Do whatever you want with the result value
console.log(result);
})