简体   繁体   English

操作顺序解析云

[英]order of operations parse cloud

I was wondering what the order of operations are in the parse cloud. 我想知道解析云中的操作顺序是什么。 I currently am running into trouble trying to do multiple things at once inside my job on the cloud. 我目前在云中的工作中尝试一次做多件事时遇到了麻烦。 I am currently trying to make an HTTP request for each user in my user table (there are 2) and then get the webpage or httprequest.text from the webpage. 我目前正在尝试为我的用户表中的每个用户发出一个HTTP请求(有2个),然后从网页上获取网页或httprequest.text。 My code is as followed 我的代码如下

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);
    }
  });
});

Where the job httpRequest is: 作业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);
    }
  });
});

now I would expect to be printed would be the, object id of first user, their url, the job running, the message"returned from http request." 现在我希望打印的将是第一个用户的对象ID,他们的url,正在运行的作业,消息“从http请求返回”。 then repeated another time for the second user and finally the job finishing with the message "Updated Records". 然后为第二个用户重复另一次,最后作业以“更新记录”消息结束。 but instead I get: 但相反,我得到:

I2014-07-22T15:15:16.013Z] A5hod7qKE3 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.045Z] http:/where.yahooapis.com/v1/places.q(德雷珀,美国)?appid =(appid)

I2014-07-22T15:15:16.053Z] GmuqxpTUpM 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.066Z] http:/where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid =(appid)

I2014-07-22T15:15:16.082Z] @Query I2014-07-22T15:15:16.082Z] @Query

I2014-07-22T15:15:16.131Z] v385: Ran cloud function httpRequest with: Input: {"url":" http://where.yahooapis.com/v1/places.q(SaltLakeCity,US)?appid=(appid) "} Result: 2487610TownSalt Lake CityUnited StatesUtahSalt LakeSalt Lake City40.777561-111.93071740.699890-112.10125740.852951-111.739479511America/Denver 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: Ran cloud function httpRequest with: Input: {"url":"' http://where.yahooapis.com/v1/places.q(Draper,US)?appid=(appid) '"} Result: 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.78304349America/Denver 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

I removed 1 / from both the printing urls so I could posts this because I don't have high enough rep to post more than 2 links. 我从两个打印网址中删除了1 /所以我可以发布这个,因为我没有足够高的代表来发布超过2个链接。 I also have put in my appid into the (appid) so the url does return to me the according woeid from yahoo.com. 我也把我的appid放入(appid)所以url确实从yahoo.com返回给我相应的woeid。 The problem here being I can't actually get into the success function of the http request job. 这里的问题是我实际上无法进入http请求作业的成功功能。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

EDIT: 编辑:

I was trying to figure out how to run a job in a for loop but couldn't get it to work. 我试图找出如何在for循环中运行一个作业,但无法让它工作。 I tried to make a promise and do what Fosco said below, but have had no luck. 我试图做出承诺并做下面Fosco所说的,但没有运气。 Here is my code. 这是我的代码。

  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(); });
    }

if I run this code I get a hello twice then the two job calls then the "got here" message once. 如果我运行这段代码,我得到一个问候两次,然后两个工作调用然后“在这里”消息一次。 I have tried adding a return statement to it and with no luck also. 我试过给它添加一个return语句,但也没有运气。 Thanks again for all the help!!! 再次感谢所有的帮助!!!

The issue here is that inside the each callback, you need to return the promise from your cloud function call if you want to ensure the tasks complete, and have it wait before going to the next object. 这里的问题是,在each回调中,如果要确保任务完成,则需要从云函数调用返回promise,并在转到下一个对象之前等待。

Simplified and using promises: 简化和使用承诺:

query.each(function(object) {
  ...
  return Parse.Cloud.run(...);
}).then(function() {
  // success
}, function(err) {
  // error
});

For looping over a promise-returning function like Parse.Cloud.run: 用于循环承诺返回函数,如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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM