如何在云代码中使用Parse promise进行循环查询
How to make queries in loop using Parse promises in cloud code?
接收到的响应始终是一个空数组。for循环中的内部stands_query永远不会被执行。此外,我想知道如果看台查询中再次出现内部查询,那么我该如何实现这一点。展位类别展位类别代码如下:
var final_list = [];
query.find().then(function(stadiums){
_.each(stadiums,function(stadium){
var stands_query = new Parse.Query(“Stands");
stands_query.equalTo(“stdId”,stadium.get(“stdId"));
var promise = stands_query.find().then(function(stands){
_.each(stands,function(stand){
var jsonObject = {
“stdId": stand.get(“stdId").id,
}
final_list.push(jsonObject);
});
return null;
},function(error){
return response.error(error);
});
return promise;
});
}).then(function(){
response.success(final_list);
});
您的第一个.then
没有返回任何内容。我会分解你的代码,这样你就可以看到:
query.find().then(function(stadiums){ //anonymous function 1
_.each(stadiums,function(stadium){ //anonymous function 2
return "foo" //this returns "foo" as a result of anonymous function 2.
});
//Nothing explicitly returned from function 1!
}).then(function(){
response.success(final_list);
});
缺少显式返回语句的函数将返回undefined
。然后,您的代码在任何内部承诺解析之前执行"response.success"。
相反,您可以创建一个内部承诺数组,等待Parse.Promise.when:
query.find().then(function(stadiums){
var promises = [];
_.each(stadiums,function(stadium){
var promise = stands_query.find().then(...)
promises.push(promise);
});
//if returning another promise, the ".then" won't execute until it completes.
return Parse.Promise.when(promises);
}).then(function(){
response.success(final_list);
});
尽管如此,根据数据集的大小,您可能会遇到超时问题。请考虑重写查询,以便使用关系查询来查询属于Stadium
的Stands
。
更新
既然您已经用字段更新了问题,那么您的行stands_query.equalTo(“stdId”,stadium.get(“stdId"));
似乎有两个错误,并且永远不会返回结果。应该是stands_query.equalTo(“stadiumId”,stadium);
。
我们有很多体育场,每个体育场都有很多看台。体育场和看台之间的关系在数据中由看台类上名为"stadiumId"的指针列表示。
在评论中,功能目标表述得非常简单:一个JSON的支架数组。这需要一个单一的查询,根本没有循环:
function allTheStands() {
var query = new Parse.Query("Stands");
query.include("stadiumId");
return query.find().then(function(stands) {
return JSON.stringify(stands);
});
}
// call it like this:
allTheStands().then(function(jsonStands) {
// jsonStands is all of the stands represented as son
});
编辑
对于相同的结果,一种更迂回的方法是在查询中不包括stadiumId,而是在stands查询完成后进行提取。
(这只是@adamdport给出的一种特定形式的建议,考虑到你的数据细节。如果你觉得这很有用,你应该相信他的回答)。
// adding underscorejs for handling arrays and other utils
var _ = require('underscore');
function allTheStands() {
var stands;
var query = new Parse.Query("Stands");
return query.find().then(function(result) {
stands = result;
// we're not done yet, because we need to fetch each stand's stadium
var promises = _.map(stands, function(stand) {
return stand.get("stadiumId").fetch().then(function(stadium) {
stand.set("stadiumId", stadium);
});
});
// as adamdport suggests, the crux of the looping answer is to use Promise.when()
return Parse.Promise.when(promises);
}).then(function() {
return JSON.stringify(stands);
});
}
相关文章:
- 使用promise和mongoose对文档进行排序
- JSON.parse没有'不能使用Javascript
- 测试Angular Service解决错误回调中的promise
- 节点协同与生成器和Promise并行流量控制
- 如何在Parse中创建一对多关系
- 根据是否解析了 Promise 从函数返回值
- 将一个方法转换为promise:Nodejs
- 如何在 Parse.com CloudCode 上创建自定义帮助程序模块以与 then() promise 一起使用?类
- Parse JavaScript SDK and Promise Chaining
- 如何使用 Parse.promise javascript 定义一个承诺数组并获取结果数组 [parse.com]
- Javascript:有没有办法为Parse.Promise.when指定无限的输入参数
- 如何在云代码中使用Parse promise进行循环查询
- 如何处理Parse promise错误,使它们不会'不要一直执行代码
- 当执行在遇到返回之前到达块的末尾时,Parse Promise中发生的事情就会被阻塞
- 如何在Parse Promise链中传递额外的数据
- Parse.Object.saveAll(objects)和Parse.Promise.when(promiseList
- 在promise中将变量设置为Parse Query的result只显示响应中的第一项
- 将列表传递给Parse.Promise.when,但只获取Promise链中的第一个元素
- 如何将回调函数包装在 Parse.Promise 中
- 如何正确使用Parse / Promise