0

When I call addCategory, it suppose to add my new category and then call _initAdminController() to go back to my main page and refresh the data there. But what is happening is getcourselist and getsubjectlist in initAdminController are somehow running first and then addCategory runs last. Do you know what can cause this? Am I using then correctly?

function _initAdminController() {
    $scope.pageIndex = "adminTab";
    console.log("reloading data");

    $http({
        method : 'GET',
        url : 'http://testserver.com:8082/getSubjectListService'
    }).then(function successCallback(response) {
        $scope.updatedSubjects = response.data;
    }, function errorCallback(response) {
        console.log(response.statusText);
    });

    $http({
        method : 'GET',
        url : 'http://testserver.hughes.com:8082/getCategoryListService'
    }).then(function successCallback(response) {
        $scope.categories = response.data;
    }, function errorCallback(response) {
        console.log(response.statusText);
    });
}

$scope.addCategory= function(){

    var name = $scope.addCategoryData.name;
    var desc = $scope.addCategoryData.description;

    $http({
        method : 'POST',
        url : 'http://testserver.com:8082/addCategoryService',
        withCredentials: true,
        cache: true,
        headers : { 'Content-Type': 'application/json' },
        data : {
            name: name,
            description: desc
        }
    }).then(_initAdminController(), function errorCallback(response) {
        console.log(response.statusText);
    });
}

Also when I use then for http get, that case is working correctly. The http get finishes first and then my scope variable get updated. And lastly before I tried the following code and in that case, successCallback doesn't run at all. So does this mean then only works for GET?

$http({
    method : 'POST',
    url : 'http://testserver.com:8082/addCategoryService',
    withCredentials: true,
    cache: true,
    headers : { 
        'Content-Type': 'application/json' 
    },
    data : {
        name: name,
        description: desc
    }
}).then(
    function successCallback(response) {
        _initAdminController()
    }, 
    function errorCallback(response) {
        console.log(response.statusText);
    }
);
3
  • Have you used the developer tools in Chrome or FF to watch what's happening and see if maybe there's an interceptor for POSTs? Commented Apr 17, 2018 at 20:01
  • 1
    You're executing _initAdminController() immediately in your .then() for the addCategory() function. Take the () off. Commented Apr 17, 2018 at 20:02
  • thanks, JC Ford, that solved my problem Commented Apr 17, 2018 at 20:19

1 Answer 1

1

The code:

}).then(_initAdminController(), ...);

Should be:

}).then(_initAdminController, ...);
Sign up to request clarification or add additional context in comments.

Comments

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.