Anson Savage
Anson Savage

Reputation: 341

No Firebase App '[DEFAULT]' has been created

I realize that that there have already been several questions that address this kind of error, but none of them seem to provide the correct solution. I'm following Stephen Grider's React Native course on Udemy.

I'm pretty sure I've followed everything exactly, so I'm guessing the problem might have to do with an update to React or Firebase or something else, but I might be completely wrong. When pressing the button that activates the following code in 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.' });
          });
      });
  }

I get the following error:

enter image description here

Since a lot of solutions on the web dealing with this error have to do with Firebase initialization, here's my code for that:

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;

Any help would be appreciated. I've been dealing with this error for about a week now, and with no help on the Udemy website, I've turned to Stack Overflow :)

Upvotes: 4

Views: 15255

Answers (5)

Moamber Raza
Moamber Raza

Reputation: 67

Try to Call all configuration of firebase in index.js (TOP LEVEL) in REACT NATIVE than share result.

Upvotes: 0

Randima Lahiru
Randima Lahiru

Reputation: 365

If you make a global reference to firebase, (ex : const storageRef = firebase.storage().ref() ), it will throw this error. But if you initialize that reference under a scope you are using, it works fine.

Upvotes: 1

Vivasonico
Vivasonico

Reputation: 11

I had a similar issue in a login redirect structure. As you state in your code ComponentWillMount is called before render, but that doesn't mean all promise are resolved when render its called. I've found a workaround by async / awaiting this method.

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'  });  }

Upvotes: 1

Anson Savage
Anson Savage

Reputation: 341

Correct the spelling of conponentWillMount to componentWillMount and correct the spelling of creatUserWithEmailAndPassword to createUserWithEmailAndPassword.

Thanks!

Upvotes: 1

Zakher Masri
Zakher Masri

Reputation: 1176

I had a similar issue and I solved it by adding a condition before the class

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

Upvotes: 1

Related Questions