我想知道解析云中的操作顺序是什么。我目前在云中的工作中尝试一次做多件事时遇到了麻烦。我目前正在尝试为我的用户表中的每个用户发出一个HTTP请求(有2个),然后从网页上获取网页或httprequest.text。我的代码如下
Parse.Cloud.job("WeatherUpdates2", function(request, status) {
var query = new Parse.Query(Parse.User);
query.exists("City");
query.each(
function(result){
var object = result;
console.log(object.id);
var city = object.get("City");
city = city.replace(" ", "");
city = city.replace(" ", "");
// get the country code.
var countryCode = object.get("CountryCode");
var woeidUrl = "http://where.yahooapis.com/v1/places.q(" + city + "," + countryCode + ")?appid=(appid)";
console.log(woeidUrl);
var woeID = "An error occured retrieving your WOEID.";
Parse.Cloud.run('httpRequest', { url: woeidUrl }, {
success: function(WOEID) {
console.log("returned from http request.");
},
error: function(error) {
console.log("Error occurred while making request for WOEID " + error.message);
status.error(error.message);
}
});
},
{
success: function() {
// results is an array of Parse.Object.
console.log('@Query');
status.success("Updated Records!!");
},
error: function(error) {
// error is an instance of Parse.Error.
console.log('@error');
response.error("Failed to save vote. Error=" + error.message);
}
});
});
作业httpRequest是:
Parse.Cloud.define("httpRequest", function(request, response) {
var webpage = "Something went wrong.";
Parse.Cloud.httpRequest({
url: request.params.url,
success: function (httpResponse) {
webpage = httpResponse.text;
webpage = webpage.toString();
response.success(webpage);
},
error: function (error)
{
console.log("Error in http request " + error.message);
response.error(error.message);
}
});
});
现在我希望打印的是第一个用户的对象ID,他们的url,正在运行的作业,“从http请求返回”的消息。然后为第二个用户重复另一次,最后作业以“更新记录”消息结束。但我得到了:
I2014-07-22T15:15:16.013Z] A5hod7qKE3
I2014-07-22T15:15:16.045Z] http:/where.yahooapis.com/v1/places.q(德雷珀,美国)?appid =(appid)
I2014-07-22T15:15:16.053Z] GmuqxpTUpM
I2014-07-22T15:15:16.066Z] http:/where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid =(appid)
I2014-07-22T15:15:16.082Z] @Query
I2014-07-22T15:15:16.131Z] v385:云端功能httpRequest用: 输入:{“url”:“http://where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid=(appid)”} 结果: 2487610TownSalt Lake CityUnited States犹他州盐湖盐湖城40.777561-111.93071740.699890-112.10125740.852951-111.739479511美国/丹佛
I2014-07-22T15:15:16.141Z] v385:云端功能httpRequest用: 输入:{“url”:“'http://where.yahooapis.com/v1/places.q(Draper,US)?appid=(appid)'”} 结果: http://where.yahooapis.com/v1/schema.rng'“xmlns:yahoo =”http://www.yahooapis.com/v1/base.rng“yahoo:start =”0“yahoo:count =” 1“yahoo:total =”11“> 2393601TownDraperUnited States犹他州Draper8402040.524139-111.86627240.442921-111.92212740.544361-111.78304349America / Denver
我从两个打印网址中删除了1 /所以我可以发布这个,因为我没有足够高的rep来发布超过2个链接。我也把我的appid放入(appid)所以url确实从yahoo.com返回给我相应的woeid。这里的问题是我实际上无法进入http请求作业的成功功能。非常感谢任何帮助!
修改
我试图弄清楚如何在for循环中运行一个作业但却无法使它工作。我试图做出承诺并做下面Fosco所说的,但没有运气。这是我的代码。
for(var i = 0; i < woeIDs.length; i++)
{
console.log("hello");
var weatherURL = "http://weather.yahooapis.com/forecastrss?w=" + woeIDs[i] + "&u=f";
var promise = Parse.Cloud.run('httpRequest', { url: weatherURL }, {
success: function(WOEID) {
console.log("got here");
},
error: function(error) {
console.log("Error occurred while making request for WOEID " + error.message);
status.error(error.message);
}
});
Parse.Promise.when([promise]).then(function() { status.success(); });
}
如果我运行此代码,我会两次打个招呼,然后两次打电话,然后“获取此处”消息一次。我试过给它添加一个return语句,但也没有运气。再次感谢所有的帮助!!!
答案 0 :(得分:3)
这里的问题是,在each
回调中,如果你想确保任务完成,你需要从你的云函数调用中返回promise,并在转到下一个对象之前让它等待。
简化并使用承诺:
query.each(function(object) {
...
return Parse.Cloud.run(...);
}).then(function() {
// success
}, function(err) {
// error
});
用于循环承诺返回函数,如Parse.Cloud.run:
var promises = [];
for (i = 0; i < 5; i++) {
promises.push(Parse.Cloud.run('...', {}));
}
Parse.Promise.when(promises).then(function() {
// all done
}, function(err) {
// error
});