简体   繁体   English

在捕获被拒绝的承诺中对Meteor.users集合执行更新操作(使用fcm-push + Meteor)

[英]Performing update operations on Meteor.users collection within catching a rejected promise (using fcm-push+Meteor)

I am utilizing an NPM package called fcm-push ( https://www.npmjs.com/package/fcm-push ) in order to send FCM notifications to various mobile devices based on a generated message. 我正在使用名为fcm-pushhttps://www.npmjs.com/package/fcm-push )的NPM软件包,以便根据生成的消息向各种移动设备发送FCM通知。 There's no issue when the FCM message successfully sends, however if the FCM message sending fails because the FCM token associated with the message is "NotRegistered," then I would like to remove the FCM token associated with the user. FCM消息成功发送时没有问题,但是如果FCM消息发送因与消息关联的FCM令牌“NotRegistered”而失败,那么我想删除与该用户关联的FCM令牌。

However, whenever the FCM message fails to send, the token never gets removed from the user's profile, even though it triggers the call back on calling Meteor.users.update . 但是,每当FCM消息无法发送时,令牌永远不会从用户的配置文件中删除,即使它在调用Meteor.users.update时触发了Meteor.users.update If there is any way for me to modify the database operation so I can successfully perform the update operation on the profile, some guidance would be appreciated. 如果有任何方法可以修改数据库操作,以便我可以在配置文件上成功执行更新操作,那么我们将不胜感激。

[INFO] -- 10:59:23 | "Error" | Data: {
  "data": "NotRegistered",
  "haltDate": "2017-03-31T10:59:23.660Z"
}  | User: cHkDSqQBMVc:APA91bFXCwp1-nxi2xxVEZARAMHs48kLm6FN0tbgmjv1lP1-LsBty_6gCFqGqDxGV9JrpCDG9pVFIxUz-77-6QxbIMa2OWmG4xoN2-E_8UoD_xe8MVoDb-DZY_KSZcMh4Bg_5F18ltg0

    return fcm.send(fcmMessage).then((data) => {
        var processEndDate = new Date();
        console.log("Response Data "+data+" ------ "+startDate+" --> "+processEndDate);
        loggerA.info("Response", {data: data, startDate: startDate, endDate: processEndDate}, token);
        return {
            method: 'SendMessage',
            status: JobberServer.Status.SUCCESS,
            dateEnd: processEndDate,
            response: data
        };
    }).catch((err) => {
        loggerA.info("Error", {data: err, haltDate: startDate}, token);
        Meteor.users.update({_id: targetId}, {$pull: {"profile.fcmTokens": {id: token}}}, {multi: true}, function (err, docsModified) {
            loggerA.info("Deregister Op", {token: token, res: err, noOfDereggedTokens: docsModified}, "NAN");
        });
        return {
            method: 'SendMessage',
            status: JobberServer.Status.FAIL,
            dateEnd: null,
            response: err
        }
    });

Ended up fixing it myself - simply just had to remove the update operation from the method itself, wrap the promise using Promise.await(...) and then return the return value of that so it can be consumed by the Meteor.call(...) callback. 结束修复它 - 只需要从方法本身删除更新操作,使用Promise.await(...)包装promise,然后返回它的返回值,以便Meteor.call(...)可以使用它Meteor.call(...)回调。 The resulting code in the method looks like this: 方法中生成的代码如下所示:

    return Promise.await(fcm.send(fcmMessage).then((data) => {
        var processEndDate = new Date();
        console.log("Response Data "+data+" ------ "+startDate+" --> "+processEndDate);
        loggerA.info("Response", {data: data, startDate: startDate, endDate: processEndDate}, token);
        return {
            status: JobberServer.Status.SUCCESS,
            response: data
        };
    }).catch((err) => {
        loggerA.info("Error", {data: err, haltDate: startDate}, token);
        return {
            status: JobberServer.Status.FAIL,
            response: err
        };
    }));

This allows me to get status and response from the response parameter of the method's call back since await waits for the promise to resolve first before returning the value. 这允许我从方法回调的响应参数中获取statusresponse ,因为await在返回值之前等待promise首先解析。 Afterwards, I can perform the necessary operations without any issue based on the response. 之后,我可以根据响应执行必要的操作而不会出现任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM