【发布时间】:2014-07-22 15:28:31
【问题描述】:
我想知道解析云中的操作顺序是什么。我目前在尝试在云上的工作中同时做多项事情时遇到了麻烦。我目前正在尝试为我的用户表中的每个用户(有 2 个)发出 HTTP 请求,然后从网页中获取网页或 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(Draper,US)?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] @查询
I2014-07-22T15:15:16.131Z] v385:运行云函数 httpRequest 与: 输入:{"url":"http://where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid=(appid)"} 结果: 2487610TownSalt Lake City美国犹他州Salt LakeSalt Lake City40.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 StatesUtahDraper8402040.524139-111.86627240.442921-111.92212740.544361-111.78304349美国/丹佛
我从两个打印网址中删除了 1 /,因此我可以发布此内容,因为我没有足够高的代表来发布超过 2 个链接。我还将我的 appid 放入 (appid) 中,因此 url 确实会从 yahoo.com 返回给我相应的 woeid。这里的问题是我实际上无法进入 http 请求作业的成功功能。非常感谢任何帮助!
编辑:
我试图弄清楚如何在 for 循环中运行作业,但无法让它工作。我试图做出承诺并按照福斯科在下面所说的做,但没有运气。这是我的代码。
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 语句,但也没有运气。再次感谢大家的帮助!!!
【问题讨论】:
-
尝试在
Parse.Cloud.run之前添加return,这样它只会在promise 被解决后继续。 -
成功了,谢谢福斯科
标签: javascript parse-platform cloud httprequest