0

I wrote the following factory service in AngularJS, but when I try to call the factory service in my RootController, I got the "undefined is not a function" error in my console.

MyService.js:

(function() {
  angular.module('serviceTestApp')
    .factory('MyService', ['$resource', '$log', '$q', '$http', MyService]);

  function MyService($log, $resource, $http, $q) {
    var name = "Tom";

    var getName = function() {
      return name;
    }; //getName

    var changeName = function(newName) {
      name = newName;
    }; //changeName

    var getIP = function() {
      var deferredObj = $q.defer();
      $resource('http://jsonip.com').query().$promise.then(function(result) {
        deferredObj.resolve(result);
      }, function(errorMsg) {
        deferredObj.reject(errorMsg);
      });

      return deferredObj.promise;
    }; //getIP

    return {
      getName: getName,
      changeName: changeName,
      getIP: getIP
    };

  }

}());

in my RootController, I try to call the services, and everything worked until I call the getIP() service - return a promise object. Does anyone see anything wrong?

RootController.js:

(function() {
  angular.module('serviceTestApp')
    .controller('RootCtrl', ['$http', '$log', '$scope', 'MyService', RootCtrl]);

  function RootCtrl($log, $scope, $http, MyService) {
    var vm = this;
    vm.message = "hello world from RootController";

    MyService.changeName("Henry Tudor");
    vm.message = "my name is: " + MyService.getName();

    MyService.getIP().query().then(function(data) {
      $log.info('in the promise, ip is: ' + data.ip);
      vm.message = vm.message + ', your IP is ' + data.ip;

    }, function(error) {
      vm.message = vm.message + ', error: ' + error;
    });

  }

}());
3
  • Did stack trace indicate a line error? - MyService.getIP() is returning a promise. Promise API does not have a query() function call - docs.angularjs.org/api/ng/service/$q Commented Jan 31, 2015 at 18:37
  • yes, the console complains about this line: $resource('jsonip.com').query().$promise.then(function(result) Commented Jan 31, 2015 at 18:39
  • Did you add your factory to your index.html file correctly, so it gets loaded ? Commented Jan 31, 2015 at 18:56

2 Answers 2

1

It may sound stupid, but I've located the cause for the error:

The API used in the $resource() returns a single JSON object:

{"ip":"2601:0:b840:8077:a97f:ee9c:f5b8:1643","about":"/about","Pro!":"http://getjsonip.com"}

however, the query() expects an array, not a JSON object. After changing to another API that returns an array in JSON format, it works. Sigh, wasted my 2 hours.

thanks everyone

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

Comments

0

in order to use your service like that

MyService.getName()

that mean you have a service called MyService and returns a function called getName

but in your case it dosen't you have to change your return to something like that

 return {
      getName: function() {return getName();},
      changeName: function() {return changeName();},
      getIP: function() {return getIP();}
    };

1 Comment

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.