Parse.com/CloudCode Promises is not entirely clear

I am stuck in this problem and could not find the answer. I wrote the following function in Cloud Code.

function getSoccerData() 
{
    console.log("Entering getSoccerData");
    var promise = Parse.Cloud.httpRequest({
        url: 'http://www.openligadb.de/api/getmatchdata/bl1/2015/'
    }).then(function(httpResponse) {
        console.log("Just a Log: " + JSON.parse(httpResponse.buffer)[1].Team1.TeamName);
        return JSON.parse(httpResponse.buffer);
    });
    return promise;
}

Hope I really used Promises the right way here.

Now I assign a function to a variable in my background job like this.

Parse.Cloud.job("updateSoccerData2", function (request, response) {

    var matchArray 
    matchArray = getSoccerData().then(function() {
        console.log("TestLog: " + matchArray[1].Team1.TeamName);
        response.success("Success!");
    }, function(error) {
        response.error(error.message);
    });
});

When I try to run this, I get the following log output

E2016-01-28T16: 28: 55.501Z] v412 Ran job updateSoccerData2 :
: {} : TypeError: 'Team1' undefined    . (_other/nunutest.js: 28: 48)      e.i(Parse.js: 14: 27703)      e.a.value(Parse.js: 14: 27063)      e.i(Parse.js: 14: 27830)      e.a.value(Parse.js: 14: 27063)      . (: 846: 17) I2016-01-28T16: 28: 55.561Z] getSoccerData I2016-01-28T16: 28: 56.920Z] : SV Darmstadt 98

, , . - ? !

+4
1

, :

Parse.Cloud.job("updateSoccerData2", function (request, response) {
    getSoccerData().then(function(matchArray) {
        console.log("TestLog: " + matchArray[1].Team1.TeamName);
        response.success("Success!");
    }, function(error) {
        response.error(error.message);
    });
});

, , .

+3

Source: https://habr.com/ru/post/1626401/


All Articles