2

私はreact-native-google-signinを使用しています。私のコードは次のとおりです。.then(this._someFunction(user));currentUserSyncの後に関数を呼び出す - Asyncは問題を待ちます

async _setupGoogleSignin() { 
    try { 
    await GoogleSignin.hasPlayServices({ autoResolve: true }); 
    await GoogleSignin.configure({ 
     webClientId: '<from web>', 
     offlineAccess: true 
    }); 

    const user = await GoogleSignin.currentUserAsync() 
     .then(this._someFunction(user)); // Is this correct? 
     console.log(user);  // this works. User is logged 
    } 

    catch(err) { 
     console.log("Play services error", err.code, err.message); 
    } 
    } 

_someFunction(user){ 

    console.log("ID: ",user.id)  // Error is thrown here 

    this.setState({id: user.id});  // This is not set 

} 

、私は機能_someFunctionuserを渡したいです。

エラーはPlay services error undefined Cannot read property 'id' of undefinedです。

GoogleSignin.currentUserAsync()が完了したときにuserを設定する関数を呼び出せます。私は間違って何をしていますか?

答えて

2

async..awaitは、通常の約束のコードとよく似ています。

.then(this._someFunction(user))が無効です。thenは、引数として関数が必要であり、undefinedを受け取るため無効です。また、userはこの時点では定義されていません。

、これはasync..awaitはですまさにです

const user = await GoogleSignin.currentUserAsync(); 
this._someFunction(user); 

でなければなりません。関数を平坦化し、実際にはthenを避ける。

関連する問題