没有Firebase应用程序' [DEFAULT]'已经被创造了

时间:2018-01-16 16:47:07

标签: react-native firebase-authentication

我意识到已经有几个问题可以解决这种错误,但它们似乎都没有提供正确的解决方案。我跟随史蒂芬格里德在Udemy的React Native课程。

我非常确定我已完全按照所有内容,所以我猜测问题可能与更新React或Firebase或其他内容有关,但我可能完全错了。当按下激活onButtonPress()中的以下代码的按钮时:

  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:)

5 个答案:

答案 0 :(得分:1)

我有一个类似的问题,我通过在课程之前添加一个条件来解决它

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

答案 1 :(得分:1)

更正conponentWillMountcomponentWillMount的拼写,并将creatUserWithEmailAndPassword的拼写更正为createUserWithEmailAndPassword

谢谢!

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

尝试在 REACT NATIVE 中调用 index.js 中的所有 firebase 配置(TOP LEVEL)而不是共享结果。