我正在尝试通过Parse Background Job向我的用户发送推送通知,如果他们在创建的宠物附近。
范围内的每个用户都与宠物交叉检查(通过日志确认),但推送通知会发送给错误的用户,或者大部分时间甚至根本不发送。我很确定我搞砸了承诺,但这里不能解决问题。
非常感谢任何帮助,谢谢!
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");
});
答案 0 :(得分:2)
你需要return
来自非常绝对的每个执行异步操作的函数的承诺。在您的情况下,您删除了query.find()
返回的承诺,并立即调用pet.save()
。我想你想把它们连在一起。
此外,您的userPromise
永远无法解决,这可能是您的连锁失败的原因。而且你的pushPromises
数组是非常不必要的,因为它最多只包含一个承诺。
此外,我使用了_.map
而不是推送到数组,并删除了您使用过的deferred antipattern。它使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");
});
});