2016-08-30 47 views
0

我與功能CanActivate一個問題()其是可變令牌不接收verifytoken總是返回未定義的函數的響應,如何解決這個問題?角2 - CanActivate未定義值

代碼

private verifytoken() 
    { 
     if(this._functionservice.getCookie("Cookie") != '' && this._functionservice.getCookie("Cookie") != "undefined") 
    { 
     this._symfonyservice.validtoken().subscribe(
      data => {this.resut = data; 
         if(this.resut['is_valid']==true) 
         { 
         console.log('true'); 

         return true; 
        } 
        else 
        { 
         this._functionservice.deleteCookie("Cookie"); 
         console.log('false'); 
         this.router.navigate(['login']); 
         return false; 
        } 
      }, 
      error =>{ 
         alert("Sessão Expirada"); 
         this._functionservice.deleteCookie("Cookie"); 
         console.log('false'); 
         this.router.navigate(['login']); 
         return false; 
        } 
      ); 

    } 
    else{ 
     this.router.navigate(['login']); 
     return false; 
    } 

    } 
    canActivate() { 
    let token = this.verifytoken(); 
    console.log(token); 
    return token; 
    } 

回答

0

我發現2個問題在這裏:

private verifytoken() 
    { 
     if(this._functionservice.getCookie("Cookie") != '' && this._functionservice.getCookie("Cookie") != "undefined") 
    { 

    // 1 - You are returning nothing here, only inside the subscriptions. So, you wont be able to get any data if you get inside this if statement 
     this._symfonyservice.validtoken().subscribe(
      data => {this.resut = data; 
         if(this.resut['is_valid']==true) 
         { 
         console.log('true'); 

         return true; 
        } 
        else 
        { 
         this._functionservice.deleteCookie("Cookie"); 
         console.log('false'); 
         this.router.navigate(['login']); 
         return false; 
        } 
      }, 
      error =>{ 
         alert("Sessão Expirada"); 
         this._functionservice.deleteCookie("Cookie"); 
         console.log('false'); 
         this.router.navigate(['login']); 
         return false; 
        } 
      ); 

    } 
    else{ 
     // 2 - you are navigating to another place, so you wont be able to get any data from here 
     this.router.navigate(['login']); 
     return false; 
    } 

    } 
    canActivate() { 
    let token = this.verifytoken(); 
    console.log(token); 
    return token; 
    } 

所以,你必須回到if語句內的訂閱或您將無法獲得任何數據。

---編輯--- 回答這個問題:

Promisses返回promisses,所以,你可以調用訂閱調用該方法後。

verifytoken()你必須返回promisse,然後在canActivate()你有像我一樣波紋管訂閱這個promisse或等待值同步:

private verifytoken(){ 
    if(this._functionservice.getCookie("Cookie") != '' && this._functionservice.getCookie("Cookie") != "undefined") 
    { 
    // here I returned the promisse 
    return this._symfonyservice.validtoken().subscribe(
     data => { 
     this.resut = data; 

     if(this.resut['is_valid']==true){ 
      console.log('true'); 
      return true; 
     } 
     else 
     { 
      this._functionservice.deleteCookie("Cookie"); 
      console.log('false'); 
      this.router.navigate(['login']); 
      return false; 
     } 
     }, 
     error =>{ 
      alert("Sessão Expirada"); 
      this._functionservice.deleteCookie("Cookie"); 
      console.log('false'); 
      this.router.navigate(['login']); 
      return false; 
     } 
    ); 

    } 
    else{ 
     this.router.navigate(['login']); 
     return false; 
    } 

} 

有一個竅門在這裏。你不能從promisse函數內部返回函數canActivate(),因爲它的範圍。所以,你有2個選項:

1 - (異步)是調用你正在調用的promisse canActivate,然後從那裏進行修改。

canActivate() { 
    return this.verifytoken(); 
} 

2 - (同步)是把一個而當promisse被觸發,響應被分配給令牌變量進行檢測,這樣你就可以返回它

canActivate() { 
    var token = this.verifytoken(); 


    while(token == undefined){ 
    // wait until token has been set 
    } 

    return token; 
} 
+0

所以,我怎麼可以等待作出迴應,然後返回true? – user26776

+0

我編輯了我的答案,讓你知道如何做到這一點。 – vinagreti