Zerok
Zerok

Reputation: 1503

Can't handle a JSON object properly

I'm doing some tests at Angular.js, and already asked here a few things. Now, I found a strange error I can't find solution for; I hope you can do different!

It's pretty simple: I use this service to retrieve a JSON (haven't tested the test part yet, but the load works perfectly):

svcs.service('dataMod', ['$http', '$q', function($http, $q){

    return{
        manage: manage
    }

    function manage(filename, action, data){
        var defered = $q.defer();
        var promise = defered.promise;

        if(action == "save"){
            $http.post(filename, data).then(function(){
                console.log("Data write success");
            }, function(){
                console.log("Error writing data");
            });
        }else{
            $http.get(filename)
                .success(function(data){
                    defered.resolve(data);
                    console.log("Data resolved");
                    console.log(">> "+JSON.stringify(data.regs));
                })
                .error(function(err){
                    defered.reject(err);
                });
            return promise;
        }
    }
}]);

I inject it into my controllers' module and use it here:

$scope.data = dataMod.manage("data/registries.json", "load");

It works because when I check it with a console.log to the $scope.data variable, I get this in the browser:

https://i.sstatic.net/Za30X.png

So you may be asking... what's the problem? Well, normally I should iterate over this JSON in a way like this: $scope.data.regs[index].object_variable, no? Or just use a ng-repeat in a way like reg in data.regs. But it just won't work.

When I try to do a console.log($scope.data.regs), I get "undefined", and I've been trying various ways to get into the info, but I never can!

What am I doing wrong here?

Upvotes: 0

Views: 82

Answers (2)

maurycy
maurycy

Reputation: 8465

I believe you have misunderstood how promises work. Promises don't resolve itself to data so them to variables will not work.

this will not work

$scope.data = dataMod.manage("data/registries.json", "load");

here the $scope.data is a promise from dataMod.manage so you could do $scope.data.then(handler, handler).finally(handler)

this will

dataMod.manage("data/registries.json", "load").then(function(fileData){
  $scope.data = fileData
})

Although as mentioned by simalexan $http returns promise it self with two special callbacks success and error

svcs.service('dataMod', ['$http', '$q',
    function ($http, $q) {
        return {
            manage: manage
        }

        function manage(filename, action, data) {
            if(action == "save") {
                return $http.post(filename, data)
            } else {
                return $http.get(filename)
            }
        }
    }
]);

and then in controller

dataMod.manage("data/registries.json", "load").then(function(response){
  $scope.data = response.data //different object structure than for success, then receives the whole response together with headers, success receives only response.data
})

Upvotes: 1

simalexan
simalexan

Reputation: 436

First:

There is no need to use $q, deferred and promise. You should just add a return before the $http.get and $http.post, as they are promises by themselves.

Second:

the $http success function returns a response with a data param. so in your case you can check the returned data with data.data.regs

And at the end inside of success block just return it like:

return data.data;

Hope this helps!

Upvotes: 1

Related Questions