Search code examples
javascriptangularjsfunctionangularjs-scope

AngularJs unable to return data from the function


I'm trying return the data from the function getShiftDetails()

SS_Mod_App.controller("SS_Ctrl", function ($scope, $http, $location, $window, $sce) {

     var ShiftDetails = []; var ShiftDtls = [];

    ShiftDtls = getShiftDetails();
      //  alert(ShiftDtls);  -->  ShiftDtls undefined
        insertDaysHtml(ShiftDtls);  

function getShiftDetails() {
        $http({
            method: 'GET',
            url: 'http://xxxxx/api/Shift/GetShiftDetails',
            params: { }
            , headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' }
        }).then(function successCallback(response) {
            ShiftDetails = response.data;               --> successful response with data     
            alert(JSON.stringify(ShiftDetails.length));    -->  getting length 21 here
            return ShiftDetails;  
        }, function errorCallback(response) {  });
    }

and want to pass it to the next function insertDaysHtml where I want iterate it and fetch data of the ShiftDetails as below but getting errors as below .

 function insertDaysHtml(ShiftDtls) {    
        alert(JSON.stringify($scope.ShiftDetails.length));   --> Length "0" here
    //alert(JSON.stringify(ShiftDtls.length)); Error msg: TypeError: Cannot read property 'length' of undefined
    // if (ShiftDtls.length > 0) {  --> Error msg: TypeError: Cannot read property 'length' of undefined

        if ($scope.ShiftDtls.length > 0) {  
           // alert(ShiftDtls.length );
            dayhtmlContent = dayhtmlContent + '<tr> '
            for (var j = 0; j <= ShiftDtls.length; j++) {
                dayhtmlContent = dayhtmlContent + '<td> $scope.ShiftDtls[ ' + j + '].ShiftName[0]));'                 
            }

        $scope.divHtmlDay = $sce.trustAsHtml(dayhtmlContent);
    }

Solution

  • First of all, you are not returning anything from getShiftDetails() function. So, you need to add a return statement like this:

    function getShiftDetails() {
      return $http({
        method: 'GET',
        url: 'http://xxxxx/api/Shift/GetShiftDetails',
        params: {},
        headers: {
          'Content-Type': 'application/json; charset=utf-8',
          'dataType': 'json'
        }
      }).then(function successCallback(response) {
        ShiftDetails = response.data;
        -- > successful response with data
        alert(JSON.stringify(ShiftDetails.length));
        -- > getting length 21 here
        return ShiftDetails;
      }, function errorCallback(response) {});
    }
    

    Secondly, your getShiftDetails() function is doing an asynchronous call using Promise. Therefore below statement is executed before getting data

    ShiftDtls = getShiftDetails();
    //  alert(ShiftDtls);  -->  ShiftDtls undefined
    insertDaysHtml(ShiftDtls); 
    //this statement is executing before getting data 
    

    So, one possible solution is call the insertDaysHtml() function from your resolved request function. Like this:

    function getShiftDetails() {
      $http({
        method: 'GET',
        url: 'http://xxxxx/api/Shift/GetShiftDetails',
        params: {},
        headers: {
          'Content-Type': 'application/json; charset=utf-8',
          'dataType': 'json'
        }
      }).then(function successCallback(response) {
        ShiftDetails = response.data;
        -- > successful response with data
        alert(JSON.stringify(ShiftDetails.length));
        -- > getting length 21 here
        insertDaysHtml(response.data); //invoke function from here
      }, function errorCallback(response) {});
    }
    

    Hope, this will help you. Thanks!