-1

I am trying to return a value after i read the data from firebase but ai am not getting it right:

This is what i have:

bill = database.ref('/restaurant/' + orderTest.restaurantKey ).once('value').then(snapchot =>{
      restaurantData = snapchot.val()
      *some functions that make billTest to 0
      billTest = 0;
      return billTest;
    });

What i get from that is this when i console log bill is this:

ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
__zone_symbol__state: true
__zone_symbol__value: 0
Symbol(Symbol.toStringTag): (...)
__proto__: Object

but i want to get just the normal value of 0.

I know that i am doing something wrong, but i tried everything that i know. I am also a begginer so i am here asking for some help

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Fábio Heitor
  • 105
  • 2
  • 12

1 Answers1

1

Data is loaded from Firebase asynchronously. The closest you can get to what you have now is by using the async / await keywords:

const snapshot = await database.ref('/restaurant/' + orderTest.restaurantKey ).once('value')

restaurantData = snapchot.val()
billTest = 0;
return billTest;

I recommend spending some time reading up on asynchronous methods calls, for example with:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807