如何在forEach中控制nodejs webpush

时间:2017-08-23 05:41:05

标签: node.js asynchronous

我正在nodejs中通过网络推送forEach发送Chrome通知,我正在更新回调中的日志表。但是,最后一条记录只会更新所有记录。如何控制这个。这是代码

records.forEach(function (record) {
    var msgD = queueRecords[nc].Message.split('####');
    var payloadMessage = "";
    if (arrSubscribeDetails[bc].Source == "Desktop") { // for Desktop
        payloadMessage = msgD[0] + '####' + msgD[2] + '####' + msgD[3] + '####' + msgD[4];
        appType = 10;
    } else { // for WAP
        payloadMessage = msgD[1] + '####' + msgD[2] + '####' + msgD[3] + '####' + msgD[4];
        appType = 1
    }
    notiType = msgD[5];
    webPush.sendNotification(arrSubscribeDetails[bc].EndPoint, {
        TTL: 345600,
        payload: payloadMessage,
        userPublicKey: arrSubscribeDetails[bc].PublicKey,
        userAuth: arrSubscribeDetails[bc].AuthenticationKey,
    }, function (subsDet) {
        var vartimeStamp = objcommon.mongoTime();
        var objectlogtable = new global.logtable();
        objectlogtable.MatriId = queueRecords[nc].MatriId;
        objectlogtable.DateUpdated = vartimeStamp;
        objectlogtable.AppType = appType;
        objectlogtable.notiType = notiType;
        objectlogtable.Status = JSONObject["responsecode"];
        insertLogTable(objectlogtable);
        if (objectlogtable.Status != 201) {
            var fieldName = 'SubscribeDetails.' + bc + '.Status';
            var fieldValue = 1;
            var queryUpdate = {};
            queryUpdate[fieldName] = fieldValue;
            updateBrowserNotificationTable(queueRecords[nc].MatriId, JSON.parse(JSON.stringify(queryUpdate)));
        }
        bc++;
        callback(splitBrowsers(function (notificationDet) {}, arrSubscribeDetails, queueRecords, nc, bc));
    });
});

0 个答案:

没有答案