gpt4 book ai didi

javascript - 解析云代码 - Promise

转载 作者:行者123 更新时间:2023-12-02 16:57:47 24 4
gpt4 key购买 nike

如果我的用户靠近创建的宠物,我会尝试通过解析后台作业向他们发送推送通知。

范围内的每个用户都会与宠物进行交叉检查(通过日志确认),但推送通知会发送给错误的用户,或者大多数时候根本不会发送。我很确定我搞砸了 promise ,但问题不能在这里。

任何帮助将不胜感激,谢谢!

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");

});

最佳答案

您需要从真正绝对每个执行异步操作的函数中返回一个 promise 。在您的例子中,您放弃了由 query.find() 返回的 promise ,并立即调用 pet.save() 。我猜你想把它们拴起来。

此外,您的 userPromise 从未得到解决,这可能是您的链失败的原因。并且您的 pushPromises 数组是完全没有必要的,因为它最多只包含一个 promise 。

另外,我使用了 _.map 而不是推送到数组,并删除了 deferred antipattern你用过的。它使返回更加突出,以便在您忘记返回时更容易发现。

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");
});
});

关于javascript - 解析云代码 - Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26040273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com