我在Parse上运行云代码。我正在运行查询,对于每个查询,我正在执行子查询。我将修改子查询的结果。但是修改了我的子查询对象后,我想修改我的查询结果对象以反映我已经处理过它们。
使用promises,我可以进行初始查询,遍历每个结果,使用该结果执行子查询并修改新的子查询结果。但是,一旦我成功保存了子查询结果,就不会调用保存回调,也无法更新我的初始查询对象。
Parse.Cloud.job("processEvents", function(request, status){
Parse.Cloud.useMasterKey();
var query = new Parse.Query("events");
query.equalTo("processed", false);
query.limit(10);
query.find().then(function(results) {
var promise = Parse.Promise.as();
_.each(results, function(eventsRecord) { //EACH
var eventData = eventsRecord.get("eventData");
var eventsObject = JSON.parse(eventData);
var events = eventsObject["events"];
for (var i = 0; i < events.length; i++) { //FOR
var currentEvent = events[i];
if(currentEvent["type"] === "vote"){
var votes = currentEvent["entity"]["votes"];
for (var i = 0; i < votes.length; i++) {
var currentVote = votes[i];
promise = promise.then(function() {
var EventAssociationObject = Parse.Object.extend("eventAssociation");
var query = new Parse.Query(EventAssociationObject);
query.equalTo("voteID", currentVote["id"]);
return query.first({
success:function(object){
var partyEntity = currentEvent["entity"];
object.set("entityJSONInformation", JSON.stringify(partyEntity));
//object in the response callback must be the object saved
return object.save(object, {
success:function(object){
eventsRecord.set("processed", true);
return eventsRecord.save();
},
error: function(error){
console.log(error);
}
});
},
error:function(error){
console.log(error);
}
});
});
};
}
}; //FOR
}); //EACH
return promise;
}).then(function() {
status.success("Finished");
});
});
答案 0 :(得分:1)
当您使用Parse的回调机制success:
执行此类操作时,您正在逃避承诺代码。当你从成功中返回时:它不会返回任何有意义的东西,也不会像承诺那样异步地等待它。
相反,你应该返回承诺并链接它们。
query.first({...
应该成为
query.first().then(function(result){
// ...
return result.save();
}).then(function(result){
eventRecords.set(...
return eventsRecord.save();
});
祝你好运,编码愉快:)。
答案 1 :(得分:0)
这是回应Benjamin Gruenbaum评论的正确代码。
Parse.Cloud.job("processEvents", function(request, status){
Parse.Cloud.useMasterKey();
var query = new Parse.Query("events");
query.equalTo("processed", false);
query.limit(10);
query.find().then(function(results) {
var promise = Parse.Promise.as();
_.each(results, function(eventsRecord) { //EACH
var eventData = eventsRecord.get("eventData");
var eventsObject = JSON.parse(eventData);
var events = eventsObject["events"];
for (var i = 0; i < events.length; i++) { //FOR
var currentEvent = events[i];
if(currentEvent["type"] === "vote"){
var votes = currentEvent["entity"]["votes"];
for (var i = 0; i < votes.length; i++) {
var currentVote = votes[i];
promise = promise.then(function() {
var EventAssociationObject = Parse.Object.extend("eventAssociation");
var query = new Parse.Query(EventAssociationObject);
query.equalTo("voteID", currentVote["id"]);
return query.first().then(function(object){}){
var partyEntity = currentEvent["entity"];
object.set("entityJSONInformation", JSON.stringify(partyEntity));
//object in the response callback must be the object saved
return object.save().then(function(object){
eventsRecord.set("processed", true);
return eventsRecord.save();
});
});
};
}
}; //FOR
}); //EACH
return promise;
}).then(function() {
status.success("Finished");
});
});