2016-05-30 66 views
0

编辑:当我最后一次在浏览器控制台中检查时,出现如下错误。

TypeError: Cannot read property 'defer' of undefined

我需要调用一个$ http请求,该请求给出了可用于调用另一个$ http请求的令牌,最后是所需的响应。 因此我使用的承诺,使其工作synchronously.However的功能没有得到的$q.defer()功能 继后执行的是我的代码:

$scope.firstTimeAuth = function($q) { 

    var deferred = $q.defer(); 
    var ref = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=https://www.googleapis.com/auth/fitness.activity.write &approval_prompt=force&response_type=code&access_type=offline', '_blank', 'location=no'); 
    ref.addEventListener('loadstart', function(event) { 

     if((event.url).startsWith("http://localhost/callback")) { 
      requestToken = (event.url).split("code=")[1]; 

      $http({ 
       method: "post", url: "https://accounts.google.com/o/oauth2/token", 
       data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=http://localhost/callback" + "&grant_type=authorization_code" + "&code=" + requestToken 
       }) 
       .success(function(data) { 
         defer.resolve(true); 
         accessToken = data.access_token; 
         refreshToken = data.refresh_token; 
         alert("firstTimeAuth success"); 

         if(typeof(Storage) != "undefined") { 
          localStorage.setItem("refreshToken",refreshToken); 
          alert(localStorage.getItem("refreshToken")); 
         } else { 
          alert("Sorry, your browser does not support Web Storage..."); 
         } 

         //functions here 
        }) 
       .error(function(data, status) { 
         alert("ERROR: " + data); 
         defer.resolve(true); 
        }); 
       ref.close(); 
      } 
     }); 
    return deferred.promise; 
    } 

This is my second function

$scope.getAcessToken = function($q) 
    { 
    var deferred = $q.defer(); 
    alert("inside getAcessToken function"); 
    refreshToken = localStorage.getItem("refreshToken"); 

    if(refreshToken) 
    { 
     $http({ 
      method: "post", url: "https://accounts.google.com/o/oauth2/token", 
      data: "client_secret=" + clientSecret + "&grant_type=refresh_token" + "&refresh_token="+ refreshToken + "&client_id=" + clientId 
       }) 
     .success(function(data){ 
      accessToken = data.access_token; 
      alert("getAcessToken success" + accessToken); 
      deferred.resolve(true); 
     }) 
     .error(function(data,status){ 
     alert("ERROR: " + JSON.stringify(data) + status); 
     deferred.resolve(true); 
     }); 
    } 
    else 
    { 
     $scope.firstTimeAuth(); 
    } 
    return deferred.promise; 
    } 

and i call them like this.

alert("not installed"); 
     var lastSaved = $scope.getFirstEpochTime(); 


     //walkthroug 
     //Registe 



     $scope.firstTimeAuth().then(function(){ 
     alert("firstime done"); 
     $scope.getDataSets().then(function(){ 
      alert(" in the last block");/* 
      $scope.handleResponse().then(function(){ 
      $scope.insert().then(function(){ 
       $scope.select(); 
       }) 
      alert("done in installed"); 
      }) 
     */}) 
     }) 

请让我知道代码有什么问题。我对此很新,谢谢。

回答

1

您是否首先在controller中注册$q

angular.module('module.name').controller('ControllerName', 
     ['$scope', '$q', function ($scope, $q) { 

}]); 

我不是真的越来越为什么你传递$q你的功能,你不需要说是。 $scope.firstTimeAuth = function($q) {

+0

是的,我已经在控制器中插入了$ q ..但是这并不帮助我们在函数参数中调用它。 – Shravan

+0

@Shravan:你不应该把它作为参数传递给你的函数,删除它......它应该工作。 – Thalaivar

+0

是的,我已经删除它。现在我不再有错误了。 但是,功能仍然没有同步调用..你可以建议任何东西。 我可以和你分享这个项目。 我真的陷入了这个问题几天了。 :( – Shravan

1

通过为一个函数定义一个参数,您正在创建一个局部变量,该变量在外部作用域中隐藏具有相同名称的任何东西。在你的情况你定义:

$scope.firstTimeAuth = function($q) {} 

然后你在很多地方调用它像$scope.firstTimeAuth();。既然你不通过任何东西,$q在函数范围内将是未定义的。您应该只将它注入整个控制器作用域中,并删除作用域方法中指定的这些参数,以便它不隐藏注入的服务。

或者如果您因某种原因必须通过它们,请正确执行。

+0

是的,我已经尝试删除我已经在函数中调用的$ q 但是功能仍然没有得到同步 感谢指出问题 我意识到现在,地方声明隐藏了声明在外部的范围内 – Shravan

+0

你可以提出任何建议吗???我可以与你分享这个项目,我真的被这个问题困住了好几天了:( – Shravan

+0

@Shravan你可能想要更新问题并解释到底你试图做什么...你似乎有访问令牌,刷新令牌,localStorage等我不确定什么是不工作的,你可以从'then()'的成功回调函数返回一个promise,并且如果你不知道,那么返回的promise只会在'.then()'返回的promise后解析。 –