0

Edit: Apologies to the answerers, it turns out that this was actually valid code, but requests were being intercepted and stripped of all of the parameters.


I'm trying to make repeated HTTP GET requests to a REST API, depending on the output and have used the solution from this question.

However, I wish to increment one of the parameters that I pass in the request. Essentially, the API pages the output and I need to increase the value of startAt accordingly.

Manual requests work fine with:

<URL>/board?startAt=50

And give back:

{"maxResults":50,"startAt":50,"isLast":true,"values":[list_of_values]}

Here's my code so far:

function getHttpPromise(start_at) {
    // This function recurses until the server returns isLast = true.
    //
    // Each iteration appends the values in response.values to
    // $scope.boards.
    test = $http({
                    url: 'boards',
                    method: 'GET',
                    params: {'startAt': start_at.toString()}
                 }).
        success(function (response) {
            console.log(response); // the response contains startAt, which always has 
                                   // the initial value (0), rather than start_at's value

            var values = response.values;
            for (var i in values) {
                var board = values[i];
                $scope.boards[board.id] = board;
            }

            if (response.isLast) {
                // We have received all the boards.
                return true;
            } else {
                // Increment start_at and return another http request promise.
                start_at += response.maxResults;
                return getHttpPromise(start_at);
            }
        }
    );

    console.log(test); // params is correct here

    return test;
}

This function is called by:

jiraWorkLog.controller('SprintSelectCtlr',
function($scope, $http, $routeParams) {
    $scope.init = function() {
        $scope.boards = new Object();

        getHttpPromise(0).then(
            function (dummy_var) {
            for (var board in $scope.boards) {
                ...
            }
        }
    );
}
...
);
5
  • what do you see in the console when you do console.log(response.maxResults); before adding it to start_at? What do you see in the when you do console.log(start_at); before and after the value is incremented? Commented Jan 23, 2017 at 15:49
  • where are you calling this getHttpPromise ?? Commented Jan 23, 2017 at 15:58
  • @Fran the value is being incremented correctly (response.maxResults=50 and start_at=0 => start_at=50 etc.). @Aravind I edited my question accordingly. It's in our SprintCtlr object. Commented Jan 23, 2017 at 16:04
  • So are you suggesting that you solved your issue or that you no longer think your question is valid? It's not really clear what your latest update is trying to say. If this isn't a valid question any longer, then you should delete it. If you solved the issue and you think the solution would be useful to others, you should provide an answer. Self answers are valid (and encouraged). Commented Jan 23, 2017 at 20:21
  • @Claies I flagged it for a moderator, since I thought they'd have a better idea than me of what the most appropriate solution was. Commented Jan 24, 2017 at 16:07

2 Answers 2

0

the response holds all the http stuff and you want to get the data out of it...

I think that the following link might help you Angular Http

another issue that you have with your recursion is that you're updating a local variable and use it wrongly...

function getHttpPromise(start_at) {
// This function recurses until the server returns isLast = true.
//
// Each iteration appends the values in response.values to
// $scope.boards.
test = $http({
                url: 'boards',
                method: 'GET',
                params: {'startAt': start_at.toString()}
             }).
    success(function (response) {
        console.log(response.data); // the response contains startAt, which always has 
                               // the initial value (0), rather than start_at's value

        var values = response.data;
        for (var i in values) {
            var board = values[i];
            $scope.boards[board.id] = board;
        }

        if (response.data.isLast) {
            // We have received all the boards.
            return true;
        } else {
            // Increment start_at and return another http request promise.
            return getHttpPromise(response.data.maxResults+1);
        }
    }
);

console.log(test); // params is correct here

return test;

}

Sign up to request clarification or add additional context in comments.

3 Comments

As I said below, the problem is formulating the request, not parsing the response. The response is valid (and correct), but the request I'm sending isn't using the correct startAt=<value> I want.
try to update your recursion call to be getHttpPromise(response.data.maxResults+1);
response.data.maxResults + 1 is always the same though (51). The value start_at is being incremented correctly, it's just not being used in the GET request.
0

The $http().success() is deprecated. You should use $http().then().

The then receives a function than will be passed a response object that has a property called data. There you'll find your values.

You can read further here

After reading your code more thoroughly, I must advise you not to solve this problem recursively. If you don't want the data paged, send a request for all the records upfront.

Now to answer why you only get the first result: it's because that's the only thing that is returned and the calling controller. You are returning a promise that the controller is waiting to be resolved. All other recursive calls happen only after your controller is already done.

9 Comments

The problem is that my request isn't being sent correctly, not getting access to the values. The request is constantly being sent with startAt=0
Now that you added the controller, I saw you are calling it with a hard-coded 0, that's why the request is always sent with 0
Running it in Chrome developer view, the value of start_at is increasing in each subsequent call (by the right amount). The 0 should just be the initial value in the first call, I thought.
Going over your question again I see plenty things wrong. I think it's a really bad practice to call REST recursively. I'm editing the answer to help you understand why you only get the first result
The recursive solution was from here: stackoverflow.com/questions/39230788/… The problem with the REST API is that there is no way of knowing how many pages there are a priori. You only know that you are finished when the response contains isLast = true.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.