Rahul
Rahul

Reputation: 183

Service not getting called in Controller

can anyone point the mistake as to why i am unable to read the Service in the Controller ? My working code is in script2.js.I am trying to create anew service github.js that will fetch username and repos_url from the github api.

Plunker link

https://plnkr.co/edit/4jnawDJgdMM61GYfpxor?p=preview

My Service github.js

The error i am getting is

TypeError: Cannot read property 'then' of undefined at ChildScope.MainController.$scope.searchFn (script3.js:33) (function() {

    var github = function($http) {

      var getuser = function(userName) {
        $http.get("https://api.github.com/users/" + userName).then(
          function(response) {
            return response.data;
          });

      };


      var getRepos = function(user1) {
        $http.get(user1.repos_url).then(
          function(response) {
            return response.data;
          });

      };

      return {
        getuser: getuser,
        getRepos: getRepos

      };


    };



    var module = angular.module("MyTestApp");
    module.factory("github", github);
}
    )();

Upvotes: 1

Views: 25

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222720

You need to return the promise from all the methods in your service,

var getuser = function(userName) {
        return  $http.get("https://api.github.com/users/" + userName).then(
          function(response) {
            return response.data;
          });

};

WORKING DEMO

Upvotes: 2

Related Questions