以下是一些部分工作和部分不工作的代码。我尝试(尽可能多地)只保留与我的问题相关的部分。看到我的担心后,代码。
Parse.Cloud.define
("myCloudFunction", function(request, response)
{
var recordTypeArray,className,recordListQuery,resultDictionary;
recordTypeArray = ["LT1","LT2","LT3","RT1","RT2","RT3"];
resultDictionary = [];
console.log("Trace-One");
watchFunction(recordTypeArray,0,resultDictionary).then
(function(resRcd) {
console.log("Trace-Two");
response.success(resultDictionary);
});
console.log("Trace-Three");
});
function watchFunction(typeArray,typeNumber,resDico)
{
var className,recordListQuery;
className = "AA_".concat(typeArray[typeNumber]).concat("_ZZ");
recordListQuery = new Parse.Query(className);
return (recordListQuery.find().then
(function(resRcd) {
// Here some problemless code.
if (typeNumber++==typeArray.length) return promise(function(){});
return watchFunction(typeArray,typeNumber,resDico)
})
);
}我在处理承诺的方式上做错了什么,但我不知道是什么。
在执行response.success.之前,我希望看到跟踪-1,然后watchFunction完成它的工作(这部分实际上很好)最后查看跟踪-2
但是,我看到的是Trace-Three,跟踪,然后我看到了,然后我可以在日志中看到watchFunction已经完成了它应该做的事情。我从没见过Trace-2。正如人们所预料到的,我收到一条消息,抱怨的成功/错误不是,所以为什么我没有看到跟踪-2并跳到我想我没有从某个地方正确地兑现承诺。我希望有人能指出我的错误在哪里。
发布于 2015-05-03 18:27:07
看起来,您希望watchFunction执行几个查询,每个可以从typeArray派生的类名都有一个查询。但是,如果这些查询成功,watchFunction将通过超出界限的typeArray索引来保证崩溃。该函数还删除结果,将其分配给一个从未引用的虚拟参数。
生成这几个查询的一个更简单的方法是将每个类名映射到查询该类的承诺。Parse.Promise.when()将运行所有查询,并由包含结果的数组(var arg)来完成。(Parse.Query.or()应该这样做,将结果合并到一个数组中)。
所以,修复watchFunction:
// create a collection of promises to query each class indicated
// by type array, return a promise to run all of the promises
function watchFunction(typeArray) {
var promises = [];
for (i=0; i<typeArray.length; ++i) {
var className = "AA_".concat(typeArray[i]).concat("_ZZ");
var recordListQuery = new Parse.Query(className);
promises.push(recordListQuery.find());
}
return Parse.Promise.when(promises);
}云函数也应该调用响应错误,可以稍微清理一下.
Parse.Cloud.define("myCloudFunction", function(request, response) {
var recordTypeArray = ["LT1","LT2","LT3","RT1","RT2","RT3"];
console.log("Trace-One");
watchFunction(recordTypeArray).then(function() {
console.log("Trace-Two");
response.success(arguments);
}, function(error) {
console.log("Trace-Two (error)");
response.error(error);
});
console.log("Trace-Three");
});您应该希望在日志中看到跟踪-1、跟踪-3、跟踪-2,因为“跟踪-2”日志会在查询完成后发生。
https://stackoverflow.com/questions/30016963
复制相似问题