AngularJS服务返回undefined

时间:2015-08-14 23:44:56

标签: javascript angularjs

我有一个名为serviceCallJson的服务从JSON文件获取数据,我有另一个服务validateIn,它根据JSON中存在的数据验证用户的输入。

(function() {
    "use strict";

  var serviceCallJson = function($http) {
      this.getCustomers = function() {
        var promise = $http({
            method : 'get',
            url : '../viewersData/userPwdPair.json'
          })

         .success(function(data) {
            return data;
          }); 
    }
  }

  var validateIn = function (serviceCallJson) {
      this.called = function(username, password) {
            this.returnedData = serviceCallJson.getCustomers();
            console.log(this.returnedData); //undefined
            var i = 0;

            angular.forEach(this.returnedData, function(value, key){
              while (i < 10) {
                if(value[i].username == username) {
                  if(value[i].password == password) {
                   alert("Logged In");
                  }
                }

                i = i + 1;
              }
            });
      }

  }

    angular.module('assignment1App')
      .service ('serviceCallJson', serviceCallJson)

    angular.module('assignment1App')
    .service ('validateIn', ['serviceCallJson', validateIn])

}())

我有以下问题:

1)我不明白为什么returnedData' is未定义when I am returning the data fetched by the serviceCallJson service on成功?

2)当console.trace()中的console时,我得到以下结果:

enter image description here

为什么我无法看到堆栈跟踪? (我希望看到我的控制器函数被调用,但它返回一个匿名函数)

第一个问题的解决方案

感谢您的所有评论。他们在调试我的应用程序时非常有帮助。以下代码使其工作。我仍然无法弄清楚第二个问题。

serviceCallJson服务

(function() {
  "use strict";

  var serviceCallJson = function($http) {
      this.getCustomers = function() {
        return $http({
            method : 'get',
            url : '../viewersData/userPwdPair.json'
          });
      };
  };

  angular.module('assignment1App')
    .service ('serviceCallJson', serviceCallJson);
}());

validateIn服务:

(function() {
  "use strict"; 

  var validateIn = function (serviceCallJson) {
      this.called = function(username, password) {

             serviceCallJson.getCustomers()
              .then(function (returnedData) {
                var i = 0;
                var j = 0;

                angular.forEach(returnedData.data, function(value){
                  while (i < 10) {
                    if(value[i].username == username) {
                      if(value[i].password == password) {
                       console.log("Logged In");
                       j = j + 1;
                      }
                    }
                    i = i + 1;
                  }

                  if (j === 0) {
                    console.log("Username or password is wrong");
                  }
                });
              });   
      };

  };
      angular.module('assignment1App')
        .service ('validateIn', ['serviceCallJson', validateIn]);
}());

更新 - 第二个问题

Console.trace()正在记录匿名函数而不是正在调用的方法名称,因为我在控制台中独立调用它。我的console.trace()服务中validate的以下用法提供了正确的堆栈跟踪,其中包含正在调用的所有函数名称和属性。

(function() {
  "use strict";

  var validate = function (fetchDataService) {
      this.verify = function(username, password) {
             console.trace();

             fetchDataService.getCustomers()
              .then(function (returnedData) {
                var i = 0;
                var count = 0;

                angular.forEach(returnedData.data, function(value){
                  while (i < 10) {
                    if(value[i].username == username) {
                      if(value[i].password == password) {
                       console.log("Logged In");
                       count = count + 1;
                      }
                    }
                    i = i + 1;
                  }

                  if (count === 0) {
                    console.log("Username or password is wrong");
                  }
                });
              });   
      };

  };
      angular.module('assignment1App')
        .service ('validate', ['fetchDataService', validate]);
}());

0 个答案:

没有答案
相关问题