Christoph
Christoph

Reputation: 1

No Data from Service to Controller to Scope -> Result Undefined Angularjs Ionic

My problem is, that the controller just send an undefiend and not the data from http of service. I inspect it with chrome. I am new at ionic. By calling the AppSqliDBFactory.getMasterdataId() method, it shows an undefiend, also at the scope variable.

.controller('ReadMasterdataCtrl', function ($scope, $state, $ionicNavBarDelegate, MasterdataService, AppSqliDBFactory){

$scope.masterdataId;
$scope.masterdataData;

AppSqliDBFactory.getMasterdataId().then( function (masterdata){
    $scope.masterdataId = masterdata[0].masterdataId;       
}).catch(function (err){
    console.log(err);
});

        //here is the error -> no data at "$scope.masterdataData = masterdata;"
        MasterdataService.getMasterdataDB($scope.masterdataId)
                .then(function (masterdata) {
                    $scope.masterdataData = masterdata;
                    console.log("getMasterdataDB respont");
                    console.log($scope.masterdataData);
                }).catch(function (err) {
            console.log(err);
        });

})

//Service
.factory('MasterdataService', function ($q, $http, SERVER_URL) {
//Create JSON Object
var srv = {};
//Array for JSON Objects
srv.masterdata = [];


srv.getMasterdataDB = function (masterdataId) {
    var deferred = $q.defer(); 

    var masterdata; 

    var masterdataId = masterdataId;

    var baseUrl = 'xxxx';



    $http.get(SERVER_URL + baseUrl + masterdataId).success(function (response){
        masterdata = response[0]; 
        console.log(masterdata);
        return deferred.resolve(masterdata);

    }).error(function (err){
        return deferred.reject(err);
    });        

    return deferred.promise;
    //return srv.getMasterdata();
};

 // Public API
return {
      getMasterdataDB: function ( masterdataId) {
        return $q.when(srv.getMasterdataDB( masterdataId));
    }
};
});

Upvotes: 0

Views: 388

Answers (1)

sp00m
sp00m

Reputation: 48837

Simplified:

AppSqliDBFactory.getMasterdataId().then(function (masterdata) {
  $scope.masterdataId = masterdata[0].masterdataId;       
});

MasterdataService.getMasterdataDB($scope.masterdataId).then(function (masterdata) {
  $scope.masterdataData = masterdata;
});

When MasterdataService.getMasterdataDB() is called, AppSqliDBFactory.getMasterdataId() may not have been resolved yet, so $scope.masterdataId can be undefined (which is probably what is happening in your case).

You have to call AppSqliDBFactory.getMasterdataId() after AppSqliDBFactory.getMasterdataId() has been resolved:

AppSqliDBFactory.getMasterdataId().then(function (masterdata) {
  $scope.masterdataId = masterdata[0].masterdataId;    
  MasterdataService.getMasterdataDB($scope.masterdataId).then(function (masterdata) {
    $scope.masterdataData = masterdata;
  });
});

Or with chaining:

AppSqliDBFactory.getMasterdataId().then(function (masterdata) {
  $scope.masterdataId = masterdata[0].masterdataId;    
  return MasterdataService.getMasterdataDB($scope.masterdataId);
}).then(function (masterdata) {
  $scope.masterdataData = masterdata;
});

Upvotes: 1

Related Questions