【问题标题】:"c.call is not a function" error with firebase in react-nativereact-native 中的 firebase 出现“c.call 不是函数”错误
【发布时间】:2019-08-05 10:29:25
【问题描述】:

我正在尝试在我的 react-native 项目中使用 firebase 来进行用户登录/注销过程。当我单击该按钮时, firebase.auth().SignInWithEmailAndPassword 函数被执行,但它给出了错误,它说“c.call 不是函数”,这发生在 auth.js 中。

我找不到任何有关该错误的相关答案。

[错误截图] (https://ibb.co/TPQCFZr)

onButtonClick() {
        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.onCreateSuccess.bind(this))
            .catch(this.onLoginFail.bind(this))
        );
    }

【问题讨论】:

    标签: javascript firebase react-native


    【解决方案1】:

    我不知道 react native,但问题可能来自您没有将回调函数传递给 then() 方法这一事实。

    尝试以下方法:

    onButtonClick() {
            const { email, password } = this.state;
    
            this.setState({ error: '', loading: true });
    
            firebase.auth().signInWithEmailAndPassword(email, password)
            .then(userCredential => this.onLoginSuccess.bind(this))
            .catch(err => {
                //err
            });
        }
    

    请参阅then() 方法here 的文档,其中说明:

    then() 方法返回一个 Promise。它最多需要两个参数: Promise 成功和失败情况的回调函数。

    并且失败情况的回调函数是可选的。


    您实际上正在使用catch() 方法做同样的事情。请参阅文档here。


    最后,请注意,您不应以相同的方式处理来自signInWithEmailAndPassword() 方法的所有错误。此类错误有多种情况:密码错误、未找到用户等。见https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#signinwithemailandpassword

    【讨论】:

    • 感谢您的回复!但这并没有解决问题..我编写了 then() 方法的内部,例如 (() => {this.....}) 但是 onLoginSuccess 方法没有绑定到 then 方法,onLoginSuccess 没有执行.
    • 您确定this 是您所期望的吗?见stackoverflow.com/questions/40340958/…
    【解决方案2】:

    您应该使用 async/await 和 还可以使用 try...catch 构造处理所有 throwable。
    示例代码:

    import auth from '@react-native-firebase/auth';
    
    async function register(email, password) {
      try {
        await auth().createUserWithEmailAndPassword(email, password);
      } catch (e) {
        console.error(e.message);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2022-06-26
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2018-01-08
      • 2021-11-18
      相关资源
      最近更新 更多