Javascript 循环中的多个查询解析云代码
我很难理解承诺,我确信我需要用它们来实现这一点,但我不知道怎么做,其他答案也帮不了我 我想循环一个数组,查询数组中每个值的所有结果,然后在计算这些结果的平均值后,在数组中添加平均值。每次迭代后,此数组作为响应发送 以下是我的代码,可以帮助理解我:Javascript 循环中的多个查询解析云代码,javascript,parse-platform,promise,parse-cloud-code,Javascript,Parse Platform,Promise,Parse Cloud Code,我很难理解承诺,我确信我需要用它们来实现这一点,但我不知道怎么做,其他答案也帮不了我 我想循环一个数组,查询数组中每个值的所有结果,然后在计算这些结果的平均值后,在数组中添加平均值。每次迭代后,此数组作为响应发送 以下是我的代码,可以帮助理解我: Parse.Cloud.define('getScorePeopleArray', function(request, response) { var peopleArray = request.params.peoplearray;
Parse.Cloud.define('getScorePeopleArray', function(request, response) {
var peopleArray = request.params.peoplearray;
var query = new Parse.Query("Scores");
var resultat;
var index, len;
var resultarray = [];
var people;
for (index = 0, len = peopleArray.length; index < len; ++index) {
people = peopleArray[index];
query.equalTo("People",people);
query.find({
success: function(results) {
var sum = 0;
for (var i = 0; i < results.length; ++i) {
sum += results[i].get("Score");
}
resultat = (sum / results.length)*5;
if(!resultat){
resultarray.push("null");
}else{
resultarray.push(resultat);
}
},
error: function() {
response.error("score lookup failed");
}
}).then();
}
response.success(resultarray);
});
Parse.Cloud.define('getScorePeopleArray',函数(请求、响应){
var peopleArray=request.params.peopleArray;
var query=newparse.query(“分数”);
结果变量;
var指数,len;
var resultarray=[];
变种人;
for(index=0,len=peopleArray.length;index
当然,response.success不是在每个查询完成时调用的,而是在尽可能快的时间调用的(因为如果我没有说错的话,查询是异步的)。
我知道我必须用承诺来改变它,但我完全不明白这是怎么回事
提前多谢 var\=require('下划线');
var _ = require('underscore');
Parse.Cloud.define('getScorePeopleArray', function(request, response) {
var peopleArray = request.params.peoplearray; // what is this an array of?
var resultArray = [];
return Parse.Promise.as().then(function() { // this just gets the ball rolling
var promise = Parse.Promise.as(); // define a promise
_.each(peopleArray, function(people) { // use underscore, its better :)
promise = promise.then(function() { // each time this loops the promise gets reassigned to the function below
var query = new Parse.Query("Scores");
query.equalTo("People", people); // is this the right query syntax?
return query.find().then(function(results) { // the code will wait (run async) before looping again knowing that this query (all parse queries) returns a promise. If there wasn't something returning a promise, it wouldn't wait.
var sum = 0;
for (var i = 0; i < results.length; i++) {
sum += results[i].get("Score");
}
var resultat = (sum / results.length) * 5;
if (!resultat){
resultArray.push("null");
} else {
resultArray.push(resultat);
}
return Parse.Promise.as(); // the code will wait again for the above to complete because there is another promise returning here (this is just a default promise, but you could also run something like return object.save() which would also return a promise)
}, function (error) {
response.error("score lookup failed with error.code: " + error.code + " error.message: " + error.message);
});
}); // edit: missing these guys
});
return promise; // this will not be triggered until the whole loop above runs and all promises above are resolved
}).then(function() {
response.success(resultArray); // edit: changed to a capital A
}, function (error) {
response.error("script failed with error.code: " + error.code + " error.message: " + error.message);
});
});
define('getScorePeopleArray',函数(请求、响应){
var peopleArray=request.params.peopleArray;//这是什么数组?
var resultArray=[];
返回Parse.Promise.as()
var promise=Parse.promise.as();//定义一个承诺
_.each(peopleArray,函数(people){//使用下划线,更好:)
promise=promise.then(function(){//每次循环时,promise都会被重新分配给下面的函数
var query=newparse.query(“分数”);
query.equalTo(“People”,People);//这是正确的查询语法吗?
return query.find().then(function(results){//代码将等待(运行异步),然后再次循环,因为知道此查询(所有解析查询)返回承诺。如果没有返回承诺的内容,则不会等待。
var总和=0;
对于(var i=0;i
你们有什么帮助吗?我会查一查,谢谢@Bergi建议大家阅读一下promise concurrence。您还应该检查Parse.Query.or(),它也对结果进行联合。太棒了,谢谢!编辑:我还将查看其他人的链接,以更好地了解承诺的工作原理,它看起来非常强大。忘记回答您的评论问题:数组是字符串数组,查询语法看起来正确(查询中的人表示数组中的每个对象,对吗?)抱歉,连续第三条消息,但我有一个错误:无法调用undefined的方法'then'。对于这些行:“return Parse.Promise.as().then(function(){…”和“.then(function(){response.success(resultaray);…”我正在研究它,我认为我刚刚修复了它。缺少一个结尾});对此表示抱歉。我仍然不工作:失败原因:ReferenceError:resultaray没有在main.js:77:22的e(Parse.js:2:5101)在Parse.js:2:4651 at Array.forEach(本机)at Object.x.each.x.forEach[as_arrayEach](Parse.js:1:665)at c.extend.resolve(Parse.js:2:4602)at Parse.js:2:5181 at e(Parse.js:2:5101)at Parse.js:4651 at Array.forEach(本机)我尝试只使用查询和缓存策略来实现这一点,以加快速度,因此,如果无法解决问题,请不要担心。这里是您获取正确行号的粘贴库: