[英]Nested queries using javascript in cloud code (Parse.com)
是否可以在云代码中执行嵌套查询?
我希望能够做类似的事情
var adList = [];
var query2 = new Parse.Query("QR");
var query = new Parse.Query("Campaigns");
query.equalTo("isFeatured", true);
query.find({
success: function(results) {
for (var i=0; i<results.length; i++){
var ad = [];
ad.push(results[i].get("Type")); //Adds "Type" to the ad array
ad.push(results[i].id); //gets the objectID and appends it to the arrayy
//second INNER QUERY
query2.equalTo("abc", true);
adList.push(ad);
query2.first({
success: function(results){
adList.push(5);
},
error: function(){
response.error("inner fail");
}
});
}
response.success(adList); //adds all ad arrays to adList array
},
error: function(){
response.error("failed");
}
});
我尝试这样做,但是内部查询从未执行。 为什么?
第二个查询是异步的,因此将其包装在for
中将不起作用。
response.success
在第二个查询完成之前被触发,因此您将在实际等待结果之前返回。 我要告诉您将response.success
移到query2
的第二次success
回调中,但这不会起作用,因为您正在for
同步运行异步操作for
因此您将在第一次success
发送响应。
不要在循环内运行异步操作 。 没用
我不确定您要在这里完成什么,但是如果您要进行与结果一样多的查询,则必须为每个调用实例化一个新的Query
。
同样,我不确定您到底想做什么,但是这是一种您可以执行以下操作的方法:
var adList = [];
var query = new Parse.Query("Campaigns");
query.equalTo("isFeatured", true);
query.find({
success: function(results) {
var queries = [];
for (var i=0; i<results.length; i++){
var query2 = new Parse.Query("QR");
query2.equalTo("abc",true);
var ad = [];
ad.push(results[i].get("Type"));
ad.push(results[i].id);
adList.push(ad);
queries.push(query2);
}
var totalLength = results.length;
function makeQueries(qs){
qs.shift().first({
success: function(currentResult) {
// do stuff with currentResult
if(qs.length){
makeQueries(qs);
} else {
console.log('We successfully made ' + totalLength + ' queries')
// we are done with the queries
response.success(adList);
}
},
error: function() {
response.error('Error in inner queries nº' + totalLength - qs.length)
}
});
}
makeQueries(queries);
},
error: function(){
response.error("failed");
}
});
请记住,“解析云代码”使您可以运行代码约5/7秒,如果results
很多,这可能会很慢。 顺便说一句,看看一个Parse的matchesQuery
方法。
从他们的文档中获取的示例:
var Post = Parse.Object.extend("Post");
var Comment = Parse.Object.extend("Comment");
var innerQuery = new Parse.Query(Post);
innerQuery.exists("image");
var query = new Parse.Query(Comment);
query.matchesQuery("post", innerQuery);
query.find({
success: function(comments) {
// comments now contains the comments for posts with images.
}
});
我认为以下代码可能会对某人有所帮助,
var adList = [];
var query2 = new Parse.Query("QR");
var query = new Parse.Query("Campaigns");
query.equalTo("isFeatured", true);
query.find().then(function(results) {
var _ = require('underscore');
var promise = Parse.Promise.as();
_.each(results, function(resultObj) {
var ad = [];
ad.push(resultObj.get("Type")); //Adds "Type" to the ad array
ad.push(resultObj.id); //gets the objectID and appends it to the arrayy
//second INNER QUERY
query2.equalTo("abc", true);
adList.push(ad);
return query2.first().then(function(results) {
adList.push(5);
});
});
return promise;
}).then(function() {
response.success(adList);
}, function (error) {
response.error("Error "+error.message);
});
我能够弄清楚以下答案。 这对我有用。 遵循parse.com文档准则中的并行承诺。 我创建了两个promise数组。 其中之一用作内部循环的promise数组。 另一个用作外部循环的promise数组。
Parse.Cloud.define("getFriends", function (request, response) {
var _ = require('underscore.js');
var queryFields = request.params.queryFields;
//var ClassToSearch = Parse.Object.extend("User");
var promises = [];
var promises2 = [];
var FinalResult = [];
var asyncFunc = function (userIDArray) {
_.each(queryFields, function (queryField) {
var query = new Parse.Query(Parse.User);
query.equalTo("yourColumnName", queryField);
promises.push(
query.find().then(function (results) {
_.each(results, function (resultObj) {
//nested query
var ClassToSearch = Parse.Object.extend("AnotherColumn");
var query2 = new Parse.Query(ClassToSearch);
query2.equalTo("yourColumnName", resultObj);
promises2.push(
query2.first().then(function (itinerary) {
FinalResults.push(itinerary);
}));
});
return Parse.Promise.when(promises2);
})
)
});
// Return a new promise that is resolved when all of the requests are done
return Parse.Promise.when(promises);
};
asyncFunc(queryFields).then(function () {
console.log("all http requests were made");
response.success(FinalResults);
}, function (error) {
console.log("there was some error");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.