Javascript 主模块无法读取我的服务

Javascript 主模块无法读取我的服务,javascript,angularjs,Javascript,Angularjs,我正在尝试调用我所做的服务中的函数。每当我试图搜索一个用户名时,我都会收到一个错误,上面写着无法读取未定义的“getUser” 它不应该是未定义的,我正在给它传递一个它应该能够使用的用户名参数。我似乎找不出我的服务有什么问题 如有任何帮助,我们将不胜感激: (function(){ var github = function($http){ var getUser = function(username){ $http.get("https://api.github.

我正在尝试调用我所做的服务中的函数。每当我试图搜索一个用户名时,我都会收到一个错误,上面写着无法读取未定义的“getUser”

它不应该是未定义的,我正在给它传递一个它应该能够使用的用户名参数。我似乎找不出我的服务有什么问题

如有任何帮助,我们将不胜感激:

(function(){

  var github = function($http){

    var getUser = function(username){
      $http.get("https://api.github.com/users/" + username)
        .then(function(response){
          return response.data; //still returns a promise
        });
    };
    //angular invokes this, return an object which is github service

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

    return {
      getUser: getUser,
      getRepos: getRepos
    };

  };

  var module = angular.module("firstapp"); //get reference to existing module, NOT creating a new one
  //register service with angular
  module.factory("github", github);
}());
script.js

var app = angular.module("firstapp", []) //defining module, no dependencies (so far)
  .controller("MainController", ["$scope", "github", "$interval", "$log", "$anchorScroll", "$location", function(
    $scope, $http, $interval, $log, $anchorScroll, $location, github) {
      $scope.search = function(username) {
        $log.info("Searching for "+username);
        //the first parameter to .then is only invokes onusercomplete if the get is successful
        //if error, it goes to second parameter which provdes error details
        github.getUser(username)
          .then(onUserComplete, onError);
        if (countdownInterval){
          $interval.cancel(countdownInterval);
          $scope.countDown = null;
        }
      };
您的问题在getUser函数中。你在那里没有兑现承诺。要按照您使用它的方式工作,它应该是

var getUser = function(username){
      return $http.get("https://api.github.com/users/" + username);
};
编辑1 在代码中,您并没有返回承诺,而是由承诺返回的数据

此外,在本例中,您将从匿名回调返回数据,然后将functionresponse{…}返回给它的调用方

编辑2

您还应该检查注入依赖项的方式

.controller("MainController", ["$scope", "github", "$interval", "$log", "$anchorScroll", "$location", function(
                                $scope, $http, $interval, $log, $anchorScroll, $location, github) {
应该是

.controller("MainController", ["$scope", '$http', "$interval", "$log", "$anchorScroll", "$location", "github", function(
                                $scope, $http, $interval, $log, $anchorScroll, $location, github) {
编辑3

您还应该更改getRepos方法,就像我们在

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


问题在于依赖项注入参数的错误序列。 它是:

应该是:

  .controller("MainController", ["$scope", "github", "$interval", "$log", "$anchorScroll", "$location","$http", function(
        $scope,github,$interval, $log, $anchorScroll, $location, github,$http)
参数在DI中的顺序必须正确

如果遇到以下错误:

"cannot read function name of undefined"
然后,您应该查找函数调用,并查看调用的对象有什么问题。
在这种情况下,github有问题

问题在于控制器定义中没有列出依赖项

 .controller("MainController", ["$scope", "github", "$interval", "$log", "$anchorScroll", "$location", function(
$scope, $http, $interval, $log, $anchorScroll, $location, github) {
您的依赖项是:

"$scope", "github", "$interval", "$log", "$anchorScroll", "$location"
但是他们也被注射了

 $scope, $http, $interval, $log, $anchorScroll, $location, github

您必须将订单行对齐,否则在错误的变量中会有错误的依赖项,而且您没有在列出的依赖项中添加$http。

我更改了此选项,以将承诺返回给调用它的script.js函数,但仍然收到未定义的错误消息。
"$scope", "github", "$interval", "$log", "$anchorScroll", "$location"
 $scope, $http, $interval, $log, $anchorScroll, $location, github