简体   繁体   中英

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. 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:

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." 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.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] @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.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

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. I also have put in my appid into the (appid) so the url does return to me the according woeid from yahoo.com. The problem here being I can't actually get into the success function of the http request job. 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. I tried to make a promise and do what Fosco said below, but have had no luck. 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. 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.

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:

var promises = [];
for (i = 0; i < 5; i++) {
  promises.push(Parse.Cloud.run('...', {}));
}
Parse.Promise.when(promises).then(function() {
  // all done
}, function(err) {
  // error
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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