避免Firebase Cloud Functions中的嵌套承诺

时间:2019-10-31 01:35:57

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

我对Javascript承诺只有有限的经验。该应用程序跟踪志愿消防员的可用性,他们可以从移动应用程序或门户网站更改其可用性状态。云功能应在消防员更新状态时执行。我希望该功能使用status: "Available"来对所有消防员进行全面统计,然后再统计该组中的驾驶员和官员。

然后,我将检查最终总数,如果这三个值中的任何一个都低于设置的阈值,那么将向所有用户发送推送通知,通知他们该数字越来越少。

我遇到的问题是数据库调用admin.firestore().collection('users').where("status", "==", "Available").get()和推送通知功能admin.messaging().send(message)都返回诺言。

如果我不嵌套承诺,则消息将在可用性计数完成之前(如预期的那样)发送。如果我将发送消息嵌套在.then( snapshot => {中,则会收到很多关于不嵌套承诺的ESLint警告

实现此目标的最佳方法是什么?我觉得我知道我需要做什么,我只是不知道该怎么做。

index.js


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const cors = require('cors')({ origin: true });

exports.sendPushNotification = functions.firestore
    .document('users/{userId}')
    .onUpdate((snap, context) => {

        // This function checks the number of available firefighters each time any firefighter
        // changes their status.  If the numbers are below the minimums specified then a push
        // notification is sent to all users alerting them of low muster numbers.


        let count = admin.firestore().collection('users').where("status", "==", "Available").get()
                    .then(snapshot => {
                        let Available = 0;
                        let Drivers = 0;
                        let Officers = 0;

                        snapshot.forEach(doc => {
                            if (doc.data().driver) {
                                Drivers++;
                            }

                            if (doc.data().officer) {
                                Officers++
                            }

                            Available++
                        });
                        return {total: Available, drivers: Drivers, officers: Officers}
                    }).catch( (error) => {
                        return console.log(error);
                    })  

            message = {
                topic: "MVFB",
                notification: {
                    title: "Warning Low Numbers",
                    body: `Critial availability - Available: ${count.total} Drivers: ${count.drivers} Officers: ${count.officers}`
                },
                android: {
                    priority: "high",
                    notification: {
                        color: "#FF0000",
                        sound: "emergency.m4a",
                        channel_id: "emergency_message"
                    }
                },
                apns: {
                    payload: {
                        aps: {
                            sound: "Emergency.aiff"
                        }
                    }
                },
            };

            admin.messaging().send(message)
            .then((response) => {
                console.log('Notification sent successfully:',response);
                return true;
            }) 
            .catch((error) => {
                console.log('Notification sent failed:',error);
                return false;
            });

        return true
    });

0 个答案:

没有答案