0

I am building a firebase cloud function that when I change a value in my db, I get UIDs of all users in db then check a node called wishlists to see if the user has products in one of his wishlists. if the wishlists of the users do not have products the cloud function sends a notification to the user with a msg to fill his wishlist. The function works for one user but when I iterate the user's nodes and call the work that I do for one user, it finishes before completing all the work. I believe that the probléme comes from not handling properly the async functions. Any help is appreciated. Thanks!

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const runtimeOpts = {
timeoutSeconds: 180,
memory: '1GB'
}

admin.initializeApp(functions.config().firebase);
exports.sendAdminNotificationForEmptyWishlists = functions.runWith(runtimeOpts)
.database.ref('/dev/fireNotifCloudFunc')
.onWrite(async () => {   
    // get all users uids
   const ref = admin.database().ref().child('Influencers')
   return ref.once("value")
   .then(function(snapshot) {
       snapshot.forEach( async function(child) {
            await checkUserWishlists(child.key)
       })
   })
   .catch(function(error){
       console.log(error); 
   });
});

//check users wishlists
async function checkUserWishlists(uid){
var ref = admin.database().ref().child('Articles').child(uid)
ref.once("value")
.then(function(snapshot) {
    if(snapshot.exists()){
        const wishlistsNumber = snapshot.numChildren();
        let wishlistCount = 0
        let userHasProduct = false         
        var BreakException = {};
        try {
            snapshot.forEach(function(child) {
                wishlistCount = wishlistCount + 1
                //wishlist node
                if(child.child("articles").exists()){
                    //user have not empty wishlist
                    userHasProduct = true
                    throw BreakException
                }
            });
        }
        catch(err){
            if (err !== BreakException) throw err;
        }
        if((wishlistCount === wishlistsNumber) && !userHasProduct){
            let fcmToken = (async () => {
                fcmToken = await getUserFirestoreFCM(uid).then(()=>{
                    sendPushNotificationToUser(fcmToken,"emptyWishlists")
                return fcmToken
                })
                .catch((error)=>{
                    console.log("error getting fcm token " + error);
                })
            })();
        }
    }
    else{
        console.log("user did not exist in articles node" );
        return 
    }
})
.catch(function(error){
    console.log(error); 
});
}

 async function getUserFirestoreFCM(uid){
   let documentRef = admin.firestore().doc("users_table/" + uid);
   documentRef.get().then(documentSnapshot => {
   if (documentSnapshot.exists) {
      console.log('Document exist')
      return  documentSnapshot.get('fcmToken')
   }
   else {
        console.log('Document dosen\'t exist')
        return 
   }
   })
   .catch(function(error){
       console.log('error geting fcm token from firestore ' + error);
   })
}  

async function sendPushNotificationToUser(fcm,type){

   //get fcm token    
     if(type === "emptyWishlists"){
        //notification content
        const payloadEmptyWishlists = {
        notification: {
        title: `Vos wishlists sont vides !`,
        body: "Hello veuillez ajouter des produits dans vos wishlists pour joindre les 
               influenceurs dans le fil d'acceil, cheers ! ",
        icon: "default",            
        sound: "default"
        }
    };
    const options = {        
        priority: "high",
        timeToLive: 60 * 60 * 24
    };
    //send notif
    return admin.messaging().sendToDevice(fcm, 
          payloadEmptyWishlists,options).then(function(response){
              console.log('Notification sent successfully:',response);
              return
    }) 
    .catch(function(error){
        console.log('Notification sent failed:',error);
    });
  }
}

this is a screeshot from the logs

  • Your use of async/await with the forEach callback isn't working the way you expect. Read this: https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – Doug Stevenson Dec 09 '19 at 15:56

1 Answers1

1

I think you should not use async/await in forEach loop callaback. This generally is tricky. Searching for good explanation I have found this article. I think its very short and clear.

In this situation I don't think that you need this async/await, but I would have to implement it to be sure. Anyway, if it appear that you still need async there is workaround in mentioned article.

vitooh
  • 4,132
  • 1
  • 5
  • 16