2017-12-13 15 views
2

私はトークンがクッキーである場合、それが正常に動作しますが、私は送信する必要がある場合const accessToken = this.getAccessToken();Returnステートメント、角度

このメソッドを呼び出すときに、私はここでは2つの方法、

private getAccessToken() { 
    let token = this.cookie.get(this.accessTokenName); 
    if (typeof token == "undefined") { 
     this.rest.get('m2/token') 
      .then((response) => { 
       if (response.token) { 
        this.cookie.put(this.accessTokenName, response.token); 
        token = response.token; 
        return token; 
       }; 
      }); 
    } else { 
     return token; 
    } 

} 

private fetchStores() { 
    const accessToken = this.getAccessToken();   
    if (!accessToken) { 
     this.core.notify('Error - Fail to connect with GWA,Try again!',0); 
    } else { 
     this.rest.get(`m2/store/?${this.accessTokenName}=${this.getAccessToken()}`).then((stores) => console.log('stores', (stores))) 
    } 
} 

を持っていますトークンを取得するためにAjax呼び出し、私はあなたがあなたの機能にコールバックを実装する必要があると思う私のaccessTokenは未定義のショーで、その後、突然、それは誤り

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.

+0

可能な重複https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an -asynchronous-call) –

+0

エラーの完全なトレースをお願いしますか? – trichetriche

答えて

1

をスローします。私はあなたの方法を変更しました。

private getAccessToken(callback) { 
     let token = this.cookie.get(this.accessTokenName); 
     if (typeof token == "undefined") { 
      this.rest.get('m2/token') 
       .then((response) => { 
        if (response.token) { 
         this.cookie.put(this.accessTokenName, response.token); 
         token = response.token; 
         callback(token); 
        }; 
       }) 
     }else{ 
      callback(token); 
     } 

    } 

    private onGetAccessToken(accessToken){ 
     if (!accessToken) { 
      this.core.notify('Error - Fail to connect with GWA,Try again!',0); 
     } else { 
      this.rest.get(`m2/store/?${this.accessTokenName}=${this.getAccessToken()}`) 
       .then((stores) => console.log('stores', (stores))) 
     } 

    } 
    private fetchStores() { 
     this.getAccessToken(onGetAccessToken);   
    } 
[私は非同期呼び出しからの応答を返すにはどうしますか?](の
関連する問題