0

I am calling this function called numberOfRedeems(dealId) from another function called setUpData. Note that the numberOfRedeems() function has a promise and returns the "counter".

When I run the same function from the setUpData function it always comes undefined. Please tell me why this is happening. Would be highly appreciated!

I suspect the problem is that there is a promise in the function that im calling.

function getNumberOfRedeems(dealId){

    userId = firebase.auth().currentUser.uid;

    var counter = 0;

    db.collection("redeemedDeals").get()
        .then(function(querySnapshot){
          querySnapshot.forEach(function(doc) {

          var data = doc.data();
          var docDealID = data.deal;

          console.log(docDealID);

          if (dealId == docDealID){
              counter = counter + 1;
          }
        });
      }).then(function(){
        console.log(counter);
        return counter;
      })

  }
function setUpData(){


    $("#uploadingProgress").hide();

    var user = firebase.auth().currentUser
    var userId = user.uid


    var userRef = db.collection('partners').doc(user.uid);
    return userRef
      .get()
      .then(doc => {
        if (doc.exists) {
          try{
          userName = doc.get("name")

          $("#partnerName").show().text(userName);

        }catch(error) {
          console.error("Could not retrieve ", error);}
        }

        db.collection("partners").doc(userId).collection("dealHistory").get()
        .then(function(querySnapshot) {

            querySnapshot.forEach(function(dealHistoryDocRef) {
                db.collection("deals").doc(dealHistoryDocRef.id).get().then(function(doc){

                      var dealId = dealHistoryDocRef.id

                      table.innerHTML +=  "<td><a id="+doc.get("id")+">"+doc.get("name")+"</a></td><td>" + getNumberOfRedeems(dealId) + "</td><td>"+doc.get("id")+"</td>"


                })
             });

        });


  })}

I expect the it to return a number, but its always undefined.

Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
Navindu09
  • 3
  • 1
  • 3

1 Answers1

1

First your func getNumberOfRedeems needs to be a Promise and return a counter from resolve.

function getNumberOfRedeems(dealId){
  return new Promise((resolve, reject) => {
    userId = firebase.auth().currentUser.uid;
    var counter = 0;
    db.collection("redeemedDeals").get()
        .then(querySnapshot => {
          querySnapshot.forEach(doc => {

          var data = doc.data();
          var docDealID = data.deal;

          console.log(docDealID);

          if (dealId == docDealID){
              counter = counter + 1;
          }
        });
      }).then(() => {
        console.log(counter);
        resolve(counter);
      })
   })
  }

Second, where you calls it, needs to be await getNumberOfRedeems and add async specific word before function declaration, like:

db.collection("deals")
  .doc(dealHistoryDocRef.id)
  .get()
  .then(async doc => {
       var dealId = dealHistoryDocRef.id
       table.innerHTML +=  "<td><a id="+doc.get("id")+">"+doc.get("name")+"</a> 
       </td><td>" + await getNumberOfRedeems(dealId) + "</td><td>"+doc.get("id")+"</td>"
})
mikeroneer
  • 40
  • 6
narcello
  • 501
  • 4
  • 11