2

I have this simple app witha factory and a controller:

angular.module('AppName', ['ngResource'])

.factory('apiData', ['$resource', function ($resource) {
    var apiRequest = $resource("https://live.reddcoin.com/api/addr/:address/balance");
    return {
        full: function(address){
            return apiRequest.get({address: address}).$promise
            .then(
                function(data){ console.log(data); return data;},
                function(){ return 'error'; }
            );
        }
        }
}])

.controller('TwoController', function($scope, apiData){
    $scope.price = apiData.full('RszZrK51ur5G67y3Wy6niTnawdYYdBRZEq').then(function(data){console.log(data); return data;});
});

The then sections in both factory and controller not returning data from the api resource. Instead it returns e { $promise=Promise, $resolved=true, toJSON=function(), more...} as can be seen in the console.

The url from the example api resource: https://live.reddcoin.com/api/addr/RszZrK51ur5G67y3Wy6niTnawdYYdBRZEq/balance

And the example on jsfiddle

0

2 Answers 2

2

I'm not sure why $resource doesn't include data(not in object format) inside object return by promise, It display result like below

e {$promise: Promise, $resolved: true} // 1003021043401956 isn't included there

I think get request is expecting object returned from the server. So if it doesn't return an object, then it will not include the same in response

There are 2 ways to solve this problem.

  1. Do return data in object format like {'data': '1003021043401956'}
  2. Create your own get request object inside resource, that will modify before it returns promise object.

    var apiRequest = $resource("https://live.reddcoin.com/api/addr/:address/balance", {}, {
       get: {
          method: 'GET',
          transformResponse: function(response){
             return {data: response}; //creating object
          }
       }
    });
    

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

.controller('TwoController', function($scope, apiData){
    apiData.full('RszZrK51ur5G67y3Wy6niTnawdYYdBRZEq').then(function(data){
        console.log(data); 
        $scope.price = data;
    });
});

Remember that promises are chained. So eventhough you return data in the success callback, the result of then is still a promise (with data as the inner result).

Working code snippet:

angular.module('AppName', ['ngResource'])

.factory('apiData', ['$resource', function ($resource) {
    var apiRequest = $resource("https://live.reddcoin.com/api/addr/:address/balance");
    return {
        full: function(address){
            return apiRequest.get({address: address}).$promise
            .then(
                function(data){ console.log(data); return data;},
                function(){ return 'error'; }
            );
        }
        }
}])

.controller('TwoController', function($scope, apiData){
    apiData.full('RszZrK51ur5G67y3Wy6niTnawdYYdBRZEq').then(function(data){console.log(data); $scope.price =  data;});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.23/angular-resource.min.js"></script>
<div ng-app="AppName" ng-controller="TwoController">{{price}}</div>

4 Comments

Yes, but data should be the returned data from the resolved promise, isn't it?
exactly, but you cannot return data within the 'then' success callback, and assign the result of 'then' to the scope. The result of 'then' is always a promise. The only way to assign the data itself to the scope, is to assign the scope variable within the then success callback.
Got it, but console.log(data); in the success callback still don't show me in the console the data I need. That's what I don't understand.
@fikkatra you know, even if OP do this $scope.price = apiData.full('RszZrK51ur5G67y3Wy6niTnawdYYdBRZEq') should work Ideally.. There is no need to have .then to wait till data comes up..

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.