0

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));
    }
};
});
1
  • $scope.masterdataId = masterdata[0].masterdataId; is working. I am getting an value. The problem is at MasterdataService.getMasterdataDB($scope.masterdataId) Commented Sep 28, 2015 at 9:55

1 Answer 1

1

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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You, I solved the problem same way. So I am really happy for your solution.
@Christoph Glad I could help, don't forget to accept it then ;)

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.