解析云代码:如何迭代 httpRequest?
Parse Cloud Code: How to iterate a httpRequest?
我正在做的是从我的 Parse 数据库加载 ID,并且我想为每个 ID 调用一个到 Facebook 的 httpRequest Api。问题是当内部没有 response.success (它结束进程)调用时,httpRequest 似乎不起作用。
这是我目前拥有的:
Parse.Cloud.define("hello", function(request, response) {
var query = new Parse.Query("Location");
query.find({
success: function(results) {
console.log(results);
for (var i = 0; i < results.length; ++i)
var locationId = results[i].get("locationId");
console.log(locationId);
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/v2.2/'+locationId+'/events?access_token='+accessToken,
success: function(httpResponse) {
console.log(httpResponse.data);
response.success("result");
},
error:function(httpResponse){
console.error(httpResponse.message);
response.error("Failed to login");
}
});
...
有什么想法吗?
我的 javascript 不是最有成就的,但我很确定这就是您正在寻找的概念。可能存在更好的解决方案。
var totalResults = results.length;
var completedResults = 0;
var completion = function() {
// Because you're doing multiple requests, you'll need to figure out how you determine success/failure. ie: Are any failures considered an overall failure? Do you want to pass a more detailed success?
response.success("Finished");
};
for (var i = 0; i < totalResults; ++i)
var locationId = results[i].get("locationId");
console.log(locationId);
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/v2.2/'+locationId+'/events?access_token='+accessToken,
success: function(httpResponse) {
completedResults++;
console.log(httpResponse.data);
if (completedResults == totalResults) {
completion();
}
},
error:function(httpResponse){
completedResults++;
console.error(httpResponse.message);
if (completedResults == totalResults) {
completion();
}
}
});
}
// ...
我正在做的是从我的 Parse 数据库加载 ID,并且我想为每个 ID 调用一个到 Facebook 的 httpRequest Api。问题是当内部没有 response.success (它结束进程)调用时,httpRequest 似乎不起作用。
这是我目前拥有的:
Parse.Cloud.define("hello", function(request, response) {
var query = new Parse.Query("Location");
query.find({
success: function(results) {
console.log(results);
for (var i = 0; i < results.length; ++i)
var locationId = results[i].get("locationId");
console.log(locationId);
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/v2.2/'+locationId+'/events?access_token='+accessToken,
success: function(httpResponse) {
console.log(httpResponse.data);
response.success("result");
},
error:function(httpResponse){
console.error(httpResponse.message);
response.error("Failed to login");
}
});
...
有什么想法吗?
我的 javascript 不是最有成就的,但我很确定这就是您正在寻找的概念。可能存在更好的解决方案。
var totalResults = results.length;
var completedResults = 0;
var completion = function() {
// Because you're doing multiple requests, you'll need to figure out how you determine success/failure. ie: Are any failures considered an overall failure? Do you want to pass a more detailed success?
response.success("Finished");
};
for (var i = 0; i < totalResults; ++i)
var locationId = results[i].get("locationId");
console.log(locationId);
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/v2.2/'+locationId+'/events?access_token='+accessToken,
success: function(httpResponse) {
completedResults++;
console.log(httpResponse.data);
if (completedResults == totalResults) {
completion();
}
},
error:function(httpResponse){
completedResults++;
console.error(httpResponse.message);
if (completedResults == totalResults) {
completion();
}
}
});
}
// ...