簡體   English   中英

操作順序解析雲

[英]order of operations parse cloud

我想知道解析雲中的操作順序是什么。 我目前在雲中的工作中嘗試一次做多件事時遇到了麻煩。 我目前正在嘗試為我的用戶表中的每個用戶發出一個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 /所以我可以發布這個,因為我沒有足夠高的代表來發布超過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語句,但也沒有運氣。 再次感謝所有的幫助!!!

這里的問題是,在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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM