Функция завершается до завершения обещаний - PullRequest
0 голосов
/ 12 февраля 2019

Я пытаюсь реализовать функцию actualSendPush, чтобы использовать для отправки 200 пользователям push-уведомлений, а затем обработать результат sendToDevice ().Проблема в том, что, несмотря на то, что я обернул весь асинхронный код с помощью Promise, облачная функция все еще завершает работу до console.log из sendToDevice (). Затем выполнения.Некоторые push-уведомления также не достигают своего пункта назначения.

При вызове actualSendPush я также использую Promise.Какой недостаток в этом коде?

function actualSendPush(fcmKey, volId, title, msg, evtId, orgId, url){

const promises = [];

console.log("notifying user about msg ", fcmKey, " ", msg);

const payload = {
    notification: {
        title: "Help Required",
        body: title,
        sound:"default",
        click_action: "FLUTTER_NOTIFICATION_CLICK"
    },
    data: {
            title : title,
            msg : msg,
            eventId : evtId
          }
};

return Promise.all([admin.messaging().sendToDevice(fcmKey, payload)
    .then(function (response) {


        // event push success counter 
        if (response.successCount == 1)
        {
            console.log("Successfully sent message:", response);
            promises.push(incrementTransaction("events/" + orgId + "/" + evtId + "/numEventsSendSuccess"));
        }
        else
        {
            console.log("Failed sent message:", response);
            promises.push(incrementTransaction("events/" + orgId + "/" + evtId + "/numEventsSendFail"));
        }

        /*var updates = {};
        updates["eventUserReqs/" + orgId + "/" + evtId + "/users/" + volId + "/status"] = response.successCount; // 0 failed 1 success
        admin.database().ref().update(updates);*/

        // The user might have opened the push before the server side had the chance to change the status to successful or not.. if it 
        // already has a value, don't change it.
        var dbRef = admin.database().ref("eventUserReqs/" + orgId + "/" + evtId + "/users/" + volId + "/status");

        promises.push(dbRef.transaction(function (current_value) {
          return current_value != null ? current_value : (response.successCount);
        }));

        return Promise.all(promises);
    })
    .catch(function (error) {
        console.log("Error sending message:", error);

        // event push fail counter
        incrementTransaction("events/" + orgId + "/" + evtId + "/numEventsSendFail");

        var dbRef = admin.database().ref("eventUserReqs/" + orgId + "/" + evtId + "/users/" + volId + "/status");
        return dbRef.transaction(function (current_value) {
          return current_value != null ? current_value : 0;
        });
    })]);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...