1

verfolge ich ein einfaches Beispiel in reagieren-native Benutzer eingeloggt sein. Ich habe folgenden Code, dassReact-Native Error „nicht definiert ist kein Objekt (Bewertung‚_this2.onLoginSuccess.bind‘)“

zu handhaben
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 
    }); 
} 

aber ich bekomme Fehler "undefined is not an object (evaluating '_this2.onLoginSuccess.bind') "

ich bin sehr neu zu reagieren-native, also bitte erklären.

enter image description here

+1

Meine Vermutung ist, dass 'onButtonPress' nicht korrekt gebunden ist, wahrscheinlich weil Sie es als Event-Handler verwenden. – Li357

Antwort

0

Die jährliche Problem, das ich hatte, ist, dass ich Methodenname falsch geschrieben. Anstatt onLoginSuccess, nannte ich es onLoginSuccess

1

Sie können nicht bind() mehrere Male so. Um bind() an Ort und Stelle ist oft und nur mit anonymer Funktion funktioniert.

Tun Sie dies statt:

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

Danke @Val. Das behebt mein Problem – pixel

+1

Oder verwenden Sie diesen Typ, um eine Funktion zu deklarieren: onLoginSuccess =() => {Stuff in your function ...}. Wenn Sie damit umgehen, ist keine "diese" Bindung notwendig. – 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 
    }); 
} 
Verwandte Themen