How to get data from {$$state: Object}?

I try to get data from database using factory. I have a problem, because I don't know how to get data from object which was returned from factory. This is what I received when I used console.log() function:

>d {$$state: Object}
 $$state: Object
  status: 1
  value: Array[1]
 __proto__: Object
 __proto__: Object

This is my factory code:

application.factory('Database', ['$http', function($http) {
   var databaseFactory = {};
    databaseFactory.get = function(query) {
        return $http.get("getData.php", {params: {'query': query}}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then(function (response) {
                databaseFactory.returnedData = response.data;
                return databaseFactory.returnedData;
            })
    }
    return databaseFactory;
}]);

And here is my controller code:

registration.controller('RegistrationController', ['$scope', 'Database', function($scope, Database) {
    $scope.posts = Database.get("SELECT * FROM `group`");
    console.log($scope.posts);
}]);

Answers:

Answer

You should pass a callback function from controller and execute it in your factory once the AJAX response is received. You can update your code to following.

Factory

application.factory('Database', ['$http', function($http) {
   var databaseFactory = {};
    databaseFactory.get = function(query, callback) {
        return $http.get("getData.php", {params: {'query': query}}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then(function (response) {
                databaseFactory.returnedData = response.data;
                callback(databaseFactory.returnedData);
            })
    }
    return databaseFactory;
}]);

Controller

registration.controller('RegistrationController', ['$scope', 'Database', function($scope, Database) {
    Database.get("SELECT * FROM `group`", function(resp){
           $scope.posts =  resp;
           console.log($scope.posts);
   });

}]);
Answer

It's returning a promise, and you need to handle it this way

In your controller add this:

getSomething.getBla().then(function(response) {

    console.log(response.data)

});

for more information: 'http://andyshora.com/promises-angularjs-explained-as-cartoon.html'

Answer

This worked in my case: result.$$state.value.

Sample Code: Controller:

promise = getConfigService.getAccount(protocol+hostName+port);
    promise
    .then(
        function (result) {
var baseUrlTemp = result.$$state.value.baseURL;
console.log("BaseURL:"+baseURLTemp)
}

Ouput: BaseURL: www.oneindia.com

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.