【问题标题】:firebase Nodejs async cloud function not finishing executionfirebase Nodejs异步云功能未完成执行
【发布时间】:2020-04-02 16:21:43
【问题描述】:

我正在构建一个 firebase 云功能,当我更改数据库中的值时,我会获取数据库中所有用户的 UID,然后检查一个名为愿望清单的节点以查看用户是否在他的愿望清单中拥有产品。如果用户的心愿单没有产品,云功能会向用户发送一条通知,其中包含一条消息以填写他的心愿单。该函数适用于一个用户,但是当我迭代用户的节点并调用我为一个用户所做的工作时,它会在完成所有工作之前完成。我相信问题来自没有正确处理异步函数。任何帮助表示赞赏。谢谢!

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

【问题讨论】:

标签: node.js firebase push-notification async-await google-cloud-functions


【解决方案1】:

我认为你不应该在forEach 循环回调中使用async/await。这通常很棘手。寻找好的解释我发现了这个article。我认为它非常简短和清晰。

在这种情况下,我认为您不需要这个async/await,但我必须实施它才能确定。无论如何,如果您似乎仍然需要异步,则在提到的文章中有解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2020-02-25
    • 2020-06-28
    • 2019-10-04
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多