没有创建Firebase应用程序“[DEFAULT]”

4

我知道已经有几个问题讨论了这种错误,但是它们似乎都没有提供正确的解决方案。我正在跟随 Stephen Grider 在 Udemy 上的 React Native 课程。

我相当确定我已经完全按照教程操作了,所以我猜测问题可能与 React 或 Firebase 的更新或其他原因有关,但我也可能完全错了。当按下激活以下代码的按钮时:

  state = { email: '', password: '', error: '' };
  //a definition of the component's state, updated with onChangeText

  onButtonPress() {
    const { email, password } = this.state;

    this.setState({ error: ' ' });

    firebase.auth().signInWithEmailAndPassword(email, password) //this line is a "promise"
      .catch(() => { //if it fails:
        firebase.auth().creatUserWithEmailAndPassword(email, password) //also returns a promoise
          .catch(() => { //if it also fails to create a username and password:
            this.setState({ error: 'Authentication Failed.' });
          });
      });
  }

我遇到了以下错误:

enter image description here

由于网络上很多处理此错误的解决方案都与Firebase初始化有关,因此这里是我的代码:
import React, { Component } from 'react';
import { View } from 'react-native';
import firebase from 'firebase';
import { Header } from './components/common/index.js'; //selects index.js automatically
import LoginForm from './components/LoginForm.js';

class App extends Component {
  conponentWillMount() {
    //called before render, will automatically be called because it's a life cycle method
    firebase.initializeApp({
      apiKey: '(I actually have my api key here, I just do not want people to steal it, same with the other values)',
      authDomain: 'authenticationtutorial-ee660.firebaseapp.com',
      databaseURL: 'my databaseURL',
      projectId: 'my projectId',
      storageBucket: 'authenticationtutorial-ee660.appspot.com',
      messagingSenderId: 'my SenderId'
  });
  }
  render() {
    return (
      <View>
        <Header headerText="Authentication" />
        <LoginForm />
      </View>
    );
  }
}

export default App;

有任何帮助将不胜感激。我已经忙碌了一个星期,处理这个错误,但在Udemy网站上没有得到任何帮助,所以我转向 Stack Overflow :)


1
App 中的 componentWillMount 拼写为 conponentWillMount,第三个字母是 n,但如果这不是问题,我在这里看不出任何错误。你是否在 Firebase 上启用了登录方法?如果没有,请前往项目->身份验证,然后启用电子邮件登录,这是第一个选项。 - K.Wu
@K.Wu 哦糟糕!我打赌那就是解决方案。我会测试并找出答案的。谢谢! - Anson Savage
1
请告诉我,因为我尝试使用不同版本的 reactreact-native 复现相同的错误,但失败了。 - K.Wu
@K.Wu 你好!我纠正了componentWillMount的拼写,这样就解决了问题。我还错拼了createUserWithEmailAndPassword,所以我也把它改正了。再次感谢你的帮助! - Anson Savage
5个回答

1

我曾经遇到过类似的问题,通过在类之前添加条件语句来解决了它

if (!firebase.apps.length) {
   firebase.initializeApp(config);
}

1

conponentWillMount 的拼写更正为 componentWillMount,将 creatUserWithEmailAndPassword 的拼写更正为 createUserWithEmailAndPassword

谢谢!


1
如果您对Firebase进行全局引用(例如:const storageRef = firebase.storage().ref()),它会抛出此错误。但是,如果您在使用的范围内初始化该引用,则可以正常工作。

1

我在登录重定向结构中遇到了类似的问题。正如您在代码中所述,ComponentWillMount在渲染之前被调用,但这并不意味着在调用渲染时所有的承诺都已经解决。我通过异步/等待此方法找到了一种解决方法。

async conponentWillMount() {
//called before render, will automatically be called because it's a life cycle method
await firebase.initializeApp({
  apiKey: '(I actually have my api key here, I just do not want people to steal it, same with the other values)',
  authDomain: 'authenticationtutorial-ee660.firebaseapp.com',
  databaseURL: 'my databaseURL',
  projectId: 'my projectId',
  storageBucket: 'authenticationtutorial-ee660.appspot.com',
  messagingSenderId: 'my SenderId'  });  }

0
尝试在React Native的index.js(顶层)中调用Firebase的所有配置,然后分享结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接