2017-09-05 3 views
1

Google認証で実行の問題を解決しようとしているとき(つまり、非同期呼び出しが完了するのを待っている)、この奇妙なエラーが発生しました。 .subscribeを追加しないとすべて動作しますが、私はGoogleのポップアップウィンドウが戻ってから別のものを続けるまで待つつもりです。Angular - TypeError:未定義の 'subscribe'プロパティを読み取ることができません

TypeError: Cannot read property 'subscribe' of undefined.

を購読一部にエラーが起こった:

this._authService.signIn().subscribe(
     value => console.log(value), 
     error => console.error(error), 
    () => console.log("done") 
    ); 
私は(それが何も返さないために使用される)、観察を返すために、「サインインを()」に変更しようとしている、私はこのエラーに遭遇しました

そして、変更されたサービスメソッド:

signIn(): Observable<boolean> { 
    const signOptions: gapi.auth2.SigninOptions = {scope: SCOPES }; 
    if (this._googleAuth) 
     Observable.fromPromise(this._googleAuth.signIn(signOptions))  
     .subscribe(response => { 
     var user:any = response; 
     if(response === true) { 
      this.handleSuccessLogin(user); 
      return Observable.of(true); 
     } 
     else { 
      return Observable.of(false); 
     } 
     }); 
    } 
    else { 
     console.error("Google Authentication not initialized"); 
     return Observable.of(false); 
    } 
    } 

アップデート:ここでサインインから直接戻って私のバージョンです。最初の提案どおり:FYI

signIn(): Observable<{}> { 
    const signOptions: gapi.auth2.SigninOptions = {scope: SCOPES }; 
    if (this._googleAuth) { 
     return Observable.fromPromise(this._googleAuth.signIn(signOptions))  
     .map(response => { 
     var user:any = response; 
     if(response === true) { 
      this.handleSuccessLogin(user); 
     } 
     return response; 
     }); 
    } 
    } 

Angular - waiting for Google authentication to complete

答えて

1

期待通りに動作しません非同期呼び出しから結果を返す:この変更につながった私の前の質問。あなたは直接観察を返し、map一部にしたい事をするthis._googleAuthブランチを変更することができます。

return Observable.fromPromise(this._googleAuth.signIn(signOptions))  
     .map(response => { 
      var user:any = response; 
      if(user) { 
      this.handleSuccessLogin(user); 
      return true; 
      } else { 
      return false; 
      } 
     }); 

Plunker demoを参照してください。

+0

ありがとうと私は例を感謝します。私はあなたの提案をしようとしていますが、何も返されておらず、handleSuccessLogin()コールは起動していません。私の更新を参照してください。私は何かを台無しにしましたか? – beachCode

+0

@beachCodeあなたはまだelseブランチを維持する必要があります。 – Pengyy

+0

@beachCodeは 'signIn():Observable <{}>'を 'signin():Observable 'に変更し、 'this._googleAuth.signIn(signOptions)'が応答を返すことを確認します。 – Pengyy

関連する問題