0

I have this function :

async useLoyaltyCard(orderTest,bill) {
    
    let restaurantKey;
    let database = firebase.database();
    let restaurantData;
    let loyaltyUseValue;
    let loyaltyCardType;

    const snapshot = await database.ref('/restaurant/' + orderTest.restaurantKey ).once('value');
      restaurantData = snapshot.val();
      loyaltyCardType = restaurantData.loyaltyCard.type.cardType;
      

      if(loyaltyCardType ==="discount"){
        console.log("TIPO DISCONTO");
      }
      if(loyaltyCardType ==="free"){
        console.log("TIPO FREE");
        bill = 0;
      }
      if(loyaltyCardType ==="points"){
        console.log("TIPO POINTS");
      }

      return bill;
  }

That should return a value of 0.

I have this on a button :

(click)="bill = this.loyaltyCardService.useLoyaltyCard(this.order,this.bill)

That is the call of the function and the use of it.

But i get this error " Assigned expression type Promise is not assignable to type numbe"

I dont know where i got the promise or what should i add to make it work

the return value is a number so i dont know why i am getting this error

Fábio Heitor
  • 105
  • 2
  • 12
  • You are declaring an async function, so the result will be a promise. – known-as-bmf Aug 11 '20 at 12:14
  • As you see, the core of your question is answered somewhere else: every async function returns a promise. I would also recommend to delegate service calls to component methods, since template should contain simple bindings to component fields and methods. You could have something like `(click)=useLoyaltycard()`, and in your component class implement the `useLoyaltyCard()` method, which will make the call to the service and store its resolved value in `this.bill`. – Dario Scattolini Aug 11 '20 at 12:21
  • I though of that and i will make that yes. But how can i get around the promise stuff? – Fábio Heitor Aug 11 '20 at 12:25
  • Your function should update the view's `bill` variable as a side effect (after resolution) instead of returning the new bill value. – known-as-bmf Aug 11 '20 at 16:53

0 Answers0