功能在所有异步工作完成之前结束

时间:2020-07-15 22:45:49

标签: javascript firebase google-cloud-firestore google-cloud-functions

所以我有一个功能可以检查订单是否存在24小时,如果是这样的话,我会向用户发送通知,但似乎并不能完成所有用户的执行,而是会返回某人其他一些则不行,我认为我在兑现承诺时遇到了问题,我不是javascript方面的专家,我也不是很了解发生了什么,有时不是尝试使用所有文档,而是如果一个文档中有deviceToken,它就完成了为空,不继续其他用户文档

exports.rememberToFinishOrder = functions.pubsub.schedule('every 3 minutes').onRun(async (context) => {
    var db = admin.firestore();
    const tsToMillis = admin.firestore.Timestamp.now().toMillis()
    const compareDate = new Date(tsToMillis - (24 * 60 * 60 * 1000)) //24 horas
    let snap = await db.collection('orders').where("timestamp","<",new Date(compareDate)).where("status", "in" ,[1,2,4,5,6]).get()
    if(snap.size > 0){
         snap.forEach(async(doc) => {
            const userId = doc.data().uid
            let userSnap = await db.collection('user').doc(userId).get()
                const deviceToken = userSnap.data().deviceToken
                const payload = {
                notification: {
                    title: "¿ Did you received your order ?",
                    body: "We need to know if you have received your order",
                    clickAction: "AppMainActivity"
                },
                data: {
                    ORDER_REMINDER: "ORDER_REMINDER"
                }
            }
            console.log("User: "+doc.data().uid)
            return admin.messaging().sendToDevice(deviceToken,payload) 
        }); 
    } 
});

有时在某些用户中,devicetoken为空时,它将完成该功能的执行,而不是继续执行下一个用户,并且对于我的订单集中的所有用户,它也不会完成此功能,它将执行某人和某人不会,这应该是一个原子操作,可以更改该集合中的所有内容,而不仅仅是某些文档

发生了什么事?

1 个答案:

答案 0 :(得分:2)

就像安德斯米哈雷斯(andresmijares)所说,您是否没有正确履行诺言。 当您执行多个异步调用时,建议使用Promise.all()函数,该函数将等待所有诺言完成之后再继续。

exports.rememberToFinishOrder = functions.pubsub.schedule('every 3 minutes').onRun(async (context) => {
    const db = admin.firestore();
    const messaging = admin.messaging();

    const tsToMillis = admin.firestore.Timestamp.now().toMillis()
    const compareDate = new Date(tsToMillis - (24 * 60 * 60 * 1000)) //24 horas
    
    const snap = await db.collection('orders').where("timestamp","<",new Date(compareDate)).where("status", "in" ,[1,2,4,5,6]).get()
    let allPromises = [];

    if(snap.size > 0){
        snap.forEach((doc) => {
            const userId = doc.data().uid;

            allPromises.push(db.collection('user').doc(userId).get().then(userSnapshot => {
                const userData = userSnapshot.data();
                const deviceToken = userData.deviceToken;
                if (userData && deviceToken) {
                    
                    const payload = {
                        notification: {
                            title: "¿ Did you received your order ?",
                            body: "We need to know if you have received your order",
                            clickAction: "AppMainActivity"
                        },
                        data: {
                            ORDER_REMINDER: "ORDER_REMINDER"
                        }
                    }
                    console.log("User: "+doc.data().uid)
                    return messaging.sendToDevice(deviceToken,payload) 
                } else {
                    return;
                }
            }));
        }); 
    }
    return Promise.all(allPromises);
});

编辑: 在发送通知之前,我添加了一个检查以查看deviceToken是否存在于userData上。