1

「未定義のオブジェクト( 『_this2.onLoginSuccess.bind』を評価する)ではありません」私は反応するネイティブの中でユーザーをログに記録するには、簡単な例を以下しています。私は リアクト - ネイティブエラーが

ことを処理するために、次のコードを追加しました
onButtonPress() { 
    const { email, password } = this.state; 
    this.setState({ error: '', loading: true }); 

    firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(this.onLoginSuccess.bind(this)) 
    .catch(() => { 
     firebase.auth().createUserWithEmailAndPassword(email, password) 
     .then(this.onLoginSucess.bind(this)) 
     .catch(this.onLoginFail.bind(this)); 
    }); 
} 

onLoginSuccess() { 
    this.setState({ 
     email: '', 
     password: '', 
     loading: false, 
     error: '' 
    }); 
} 

onLoginFail() { 
    this.setState({ 
     error: 'Authentication Failed', 
     loading: false 
    }); 
} 

が、私は私が反応し、ネイティブのは非常に新しいです"undefined is not an object (evaluating '_this2.onLoginSuccess.bind')

エラーを取得し、そう説明してください。

enter image description here

+1

おそらく、あなたがイベントハンドラとして使っているので、おそらく 'onButtonPress'が正しくバインドされていないと思います。 – Li357

答えて

0

年間の問題は、私はメソッドの名前をスペルが間違っていることです。代わりにonLoginSuccess、私はそれをonLoginSucessと呼んだ

1

bind()のように複数回はできません。代わりにbind()には、匿名機能でのみ動作します。

代わりにこれを行います。私が持っていた

constructor(props) { 
    super(props); 

    this.onLoginSuccess = this.onLoginSuccess.bind(this); 
    this.onLoginFailed = this.onLoginFailed.bind(this); 
} 

onButtonPress() { 
    const { email, password } = this.state; 
    this.setState({ error: '', loading: true }); 

    firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(this.onLoginSuccess) 
    .catch(() => { 
     firebase.auth().createUserWithEmailAndPassword(email, password) 
     .then(this.onLoginSucess) 
     .catch(this.onLoginFail); 
    }); 
} 

onLoginSuccess() { 
    this.setState({ 
     email: '', 
     password: '', 
     loading: false, 
     error: '' 
    }); 
} 

onLoginFail() { 
    this.setState({ 
     error: 'Authentication Failed', 
     loading: false 
    }); 
} 
+0

ありがとう@Val。それは私の問題を修正しました – pixel

+1

または、このタイプを使用して関数を宣言してください:onLoginSuccess =()=> {あなたの関数内のもの...}。あなたがそれをこのように扱うなら、 "この"束縛は必要ではありません。 – Dedpul

0
onButtonPress() { 
    const { email, password } = this.state; 
    this.setState({ error: '', loading: true }); 

    firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(this.onLoginSuccess.bind(this)) 
    .catch(() => { 
     firebase.auth().createUserWithEmailAndPassword(email, password) 
     .then(this.onLoginSucess.bind(this)) 
     .catch(this.onLoginFail.bind(this)); 
    }); 
} 

onLoginSuccess =() => { 
    this.setState({ 
     email: '', 
     password: '', 
     loading: false, 
     error: '' 
    }); 
} 

onLoginFail =() => { 
    this.setState({ 
     error: 'Authentication Failed', 
     loading: false 
    }); 
} 
関連する問題