babbaggeii
babbaggeii

Reputation: 7737

Http call being made twice with parameters reset

I'm making a GET request to retrieve posts, but when I load the page, it makes the first request with the correct page query in the request URL but then immediately makes another request with page set to 1. Here's the console output when I make a request for page=3:

"page: 3"
bolt.js:54 Object {method: "GET", url: "http://localhost/api/v1/content/news?order=datepublish%20DESC&limit=10&page=3", headers: Object}
bolt.js:54 Object {method: "GET", url: "http://localhost/api/v1/content/news?order=datepublish%20DESC&limit=10&page=1", headers: Object}
bolt.js:58 "success function called."
posts.controller.js:35 Object {data: Object, status: 200, headers: function, config: Object, statusText: "OK"}
bolt.js:58 "success function called."

So you can see it's clearly making two GET requests, but only returning one set of data (the latter, with page=1.

In my posts.controller.js, I have:

activate();

function activate() {

    $scope.isLoading = 1;
    $scope.previousLink = 0;

    return getPosts($stateParams.page).then(function(data) {
        $scope.isLoading = 0;
        $rootScope.pageLoading = 0;
    });
}

function getPosts(page) {

    console.log("page: " + page);

    var contenttype = 'news';
    var order = 'datepublish%20DESC';
    var limit = 10;

    return Bolt.getRecords(contenttype, order, limit, page)
        .then(function(data){

            // Below is line 35
            console.log(data);
            $scope.posts = data.data.data;

        });
}

And the Bolt service (bolt.js):

function getRecords(contenttype, order, limit, page) {

    var request = {
        method: 'GET',
        url:    API_BASE_URL + 'content/' + contenttype +
                    '?order=' + order +
                    '&limit=' + limit +
                    '&page=' + page,
        headers: {
            'Content-Type':     'application/json',
            'Accept':           'application/json'
        }
    };

    // Below is line 54
    console.log(request);

    return $http(request)
        .success(function(data){

            // Below is line 58
            console.log("success function called.");                        
            return data;
        })
        .error(function(error){
            return error;
        });
}

What am I doing wrong here?

Upvotes: 0

Views: 440

Answers (1)

LCO TEC BAJA
LCO TEC BAJA

Reputation: 684

Sometimes there are asynchronous function calls made within a watch that are executed via the digest cycle.

Check if you have used your getRecords or getPosts function anywhere else in your code. Perhaps is related to watching some variable related to your pages parameter.

Good luck.

Upvotes: 1

Related Questions