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

neptune
  • 1,211
  • 2
  • 19
  • 32

2 Answers2

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

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
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>
fikkatra
  • 5,605
  • 4
  • 40
  • 66
  • Yes, but `data` should be the returned data from the resolved promise, isn't it? – neptune Apr 14 '16 at 10:04
  • 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. – fikkatra Apr 14 '16 at 10:06
  • 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. – neptune Apr 14 '16 at 10:09
  • @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.. – Pankaj Parkar Apr 14 '16 at 10:16