0

I use get() from firebase in a function but it can only log in the console the data from the database. Is it possible to get back the data in the funcntion ? Here is the code :

function readAccount(userId) {
    const dbRef = ref(getDatabase());
    get(child(dbRef, `users/${userId}/bank:/credit`)).then((snapshot) => {
        if (snapshot.exists()) {
          console.log(snapshot.val());
          return snapshot.val();
        } else {
          console.log("No data available");
        }
      }).catch((error) => {
        console.error(error);
      });
}

1 Answers1

0

Try returning the result of the get function, like this:

function readAccount(userId) {
    const dbRef = ref(getDatabase());
    return get(child(dbRef, `users/${userId}/bank:/credit`)).then((snapshot) => {
        if (snapshot.exists()) {
          console.log(snapshot.val());
          return snapshot.val();
        } else {
          console.log("No data available");
        }
      }).catch((error) => {
        console.error(error);
      });
}

Depending on what you're building this in you could also assign the result of the get function to a variable defined elsewhere.

jackstruck
  • 11
  • 2