Firebase云功能使用Promise.all时不起作用

时间:2017-07-20 21:37:51

标签: javascript android firebase firebase-cloud-messaging google-cloud-functions

我有一个Android应用程序来组织活动 我正在尝试向订阅者发送有关活动时间或日期变化的通知。 当我使用以下示例时,该函数工作正常

//Cloud Functions Modules
const functions = require('firebase-functions');
//Firebase Admin SDK Modules (it will send the Notifications to the user)
const admin = require('firebase-admin');
//init Admin SDK
admin.initializeApp(functions.config().firebase);

exports.changeventTime = functions.database.ref('/user-                
event/{authUid}/{key}/eventTime/')
    .onWrite(event => {
        var eventKey = event.params.key;
        var authUid = event.params.authUid;


    var eventSnapshot = event.data;

    var newTime = eventSnapshot.val();

    var eventTopic = "notifications_"+eventKey;






    var payload = {
        data: {
            pushTyp: 'changTime',
            time: newTime,
            key: eventKey,
            authuid: authUid




        }
    };

    // Send a message to devices subscribed to the provided topic.
    return admin.messaging().sendToTopic(eventTopic, payload)
        .then(function (response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log("Successfully sent message:", response);
        })
        .catch(function (error) {
            console.log("Error sending message:", error);
        });

    });

但是当我尝试使用"返回Promise.all()"功能不起作用! 如下例所示:

//Cloud Functions Modules
const functions = require('firebase-functions');
//Firebase Admin SDK Modules (it will send the Notifications to the user)
const admin = require('firebase-admin');
//init Admin SDK
admin.initializeApp(functions.config().firebase);

exports.changeventTime = functions.database.ref('/uesr-        
event/{authUid}/{eventKey}/eventTime/')
    .onWrite(event => {
        const eventKey = event.params.eventKey;
        const authUid = event.params.authUid;

        const eventTopic = "notifications_"+eventKey;



    const eventSnapshot = event.data;

    const newTime = eventSnapshot.val();

    const getevent = admin.database().ref(`user-event/${authUid}/${eventKey}/`).once('value');
    return Promise.all(getevent).then(results => {
        const eventSnapshot = results[0];
            const eventNumber = eventSnapshot.val().eventNumber;
            const eventDescription = eventSnapshot.val().eventDescription;
            const eventTime = eventSnapshot.val().eventTime;







        const payload = {
        data: {
            pushTyp: 'changeTime',
            time: eventTime,
            key: eventKey,
            authuid: authUid,
            number: eventNumber,
            dscr: eventDescription




        }
     };

     // Send a message to devices subscribed to the provided topic.
     return admin.messaging().sendToTopic(eventTopic, payload)
        .then(function (response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log("Successfully sent message:", response);
        })
        .catch(function (error) {
            console.log("Error sending message:", error);
        });


    });









    });

0 个答案:

没有答案