React-Native Error“undefined不是对象(评估'_this2.onLoginSuccess.bind')”

时间:2017-12-20 02:59:46

标签: reactjs react-native react-native-android react-native-ios

我正在使用react-native中的一个简单示例来记录用户。我添加了以下代码来处理

    // Print word frequencies
    wordCount.forEach((k, v) -> {
        if(k.equalsIgnoreCase(key))
        System.out.printf("%s %d\n", k, v);
    });

但我收到错误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 }); }

我很反应原生,所以请解释。

enter image description here

3 个答案:

答案 0 :(得分:2)

你不能多次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
    });
}

答案 1 :(得分: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
    });
}

答案 2 :(得分:0)

我遇到的年度问题是我拼写错误的方法名称。而不是onLoginSuccess,我把它称为onLoginSucess