简体   繁体   English

解析云代码-承诺

[英]Parse Cloud Code - Promises

I'm trying to send push notifications to my users via Parse Background Job if they are in proximity of the pet that was created. 我正在尝试通过Parse Background Job向我的用户发送推送通知,如果他们在所创建的宠物附近。

Every user in range gets crosschecked with the pets (confirmed via log) but the push notifications are sent to the wrong user or most of the time not even sent at all. 范围内的每个用户都会与宠物进行交叉检查(通过日志确认),但推送通知会发送给错误的用户,或者在大多数情况下甚至根本不会发送。 I'm pretty sure I messed the promises up but can't the problem here. 我敢肯定,我兑现了诺言,但是这里不能解决问题。

Any help would be much appreciated, thanks! 任何帮助将不胜感激,谢谢!

Parse.Cloud.job("locationPush", function(request, status) {

Parse.Cloud.useMasterKey();

var Pet = Parse.Object.extend("Pet");
var petQuery = new Parse.Query(Pet);
petQuery.equalTo("initialPushSent", false);
petQuery.equalTo("status", "missing");
petQuery.equalTo("deleted", false);

petQuery.find().then(function(pets) {
  var petPromises = [];
  _.each(pets, function(pet) {
    console.log("checking pet: " + pet.id);

    var petLocation = pet.get("lastSeenLocation");
    var query = new Parse.Query(Parse.User);
    query.withinKilometers("lastLocation", petLocation, 50);

    query.find().then(function(users) {

        var userPromises = [];

        _.each(users, function(user) {

            var userPromise = new Parse.Promise();
            userPromises.push(userPromise);

            console.log("check user " + user.id + " with pet: " + pet.id);

            var pushPromises = [];

            if(petLocation.kilometersTo(user.get("lastLocation")) <= user.get("pushRadius")){

                console.log("send push to" + user.id);

                var promise = new Parse.Promise();
                pushPromises.push(promise);

                Parse.Push.send({
                    channels: [ "user_" + user.id ],
                    data: {
                        alert : "Neues vermisstes Tier im Umkreis"
                    }}, 
                    { success: function() {
                        console.log("push sent to: " + user.id)
                    }, 
                    error: function(error) {
                        console.log("error sending push: " + error)

                    }}).then (function(result){
                        promise.resolve();
                    }, function(error) {
                        promise.reject();
                    });

                }

                return Parse.Promise.when(pushPromises);


            });


        return Parse.Promise.when(userPromises);

    });

    petPromises.push(pet.save());
  });

  return Parse.Promise.when(petPromises);

}).then(function() {
    status.success("location Send complete"); 

}, function(error) {
    status.error("location Send Error"); 

});

You need to return a promise from really absolutely every function that does something asynchronous. 您实际上绝对需要从执行异步操作的每个函数中return承诺。 In your case, you dropped the promise that was returned by query.find() , and called pet.save() immediately. 在您的情况下,您删除了由query.find()返回的query.find() ,并立即调用了pet.save() I guess you wanted to chain them. 我想您想将它们链接起来。

Also, your userPromise s were never resolved, which likely is the reason that your chain failed. 另外,您的userPromise从未解决,这很可能是您的链失败的原因。 And your pushPromises array is quite unnecessary, as it only will contain at most one promise. 而且您的pushPromises数组是不必要的,因为它最多包含一个承诺。

Also, I've used _.map instead of pushing to arrays, and removed the deferred antipattern that you had used. 另外,我使用_.map而不是推送到数组,并删除了您已使用的延迟反模式 It makes the returns more prominent, so that it's easier to spot if you forgot one. 它使returns更加显着,因此,如果您忘记了returns则更容易发现。

Parse.Cloud.job("locationPush", function(request, status) {
    Parse.Cloud.useMasterKey();
    var Pet = Parse.Object.extend("Pet");
    var petQuery = new Parse.Query(Pet);
    petQuery.equalTo("initialPushSent", false);
    petQuery.equalTo("status", "missing");
    petQuery.equalTo("deleted", false);
    return petQuery.find().then(function(pets) {
        return Parse.Promise.when(_.map(pets, function(pet) {
            console.log("checking pet: " + pet.id);
            var petLocation = pet.get("lastSeenLocation");
            var query = new Parse.Query(Parse.User);
            query.withinKilometers("lastLocation", petLocation, 50);
            query.find().then(function(users) {
                return Parse.Promise.when(_.map(users, function(user) {
                    console.log("check user " + user.id + " with pet: " + pet.id);

                    if (petLocation.kilometersTo(user.get("lastLocation")) <= user.get("pushRadius")) {
                        console.log("send push to" + user.id);
                        return Parse.Push.send({
                            channels: ["user_" + user.id],
                            data: {
                                alert: "Neues vermisstes Tier im Umkreis"
                            }
                        }, {
                            success: function() {
                                console.log("push sent to: " + user.id)
                            },
                            error: function(error) {
                                console.log("error sending push: " + error)
                            }
                        }); // we already got a promise here!
                    } else
                        return null;
                }));
            }).then(function() {
                return pet.save();
            });
        }));
    }).then(function() {
        status.success("location Send complete");
    }, function(error) {
        status.error("location Send Error");
    });
});

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

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