0
votes

I have the Firebase Emulator running locally. I am trying to connect Firebase Emulator with the iPhone app written in React Native.

Following is the code snippet:

import firestore from '@react-native-firebase/firestore'

const App = () => {
  const db = firestore();
  db.settings({host: 'localhost:8080', ssl: false});
  const collectionRef = firestore().collection('test');
  const userDocument = collectionRef
    .doc()
    .set({
      name: 'John',
    })
    .catch(function (error) {
      console.log('Errror:', error);
    });
  return (
    <View>
      <Text>Testing
</Text>
    </View>
  );
};

When I run the code I get the following error.

Error: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()

But the Firebase documentation does not mention initializeApp.

I'm not sure what is missing, I appreciate your help. I'm new to React Native, and I'm sure others will have this question in the future.

1

1 Answers

1
votes

Yep, you have to initialize...

The best way is to create a const with your firebaseConfig data and initialize like so: firebase.initializeApp(firebaseConfig)

Example:

const firebaseConfig = {
  ...
  databaseURL: "https://XXX.firebaseio.com",
  projectId: "XXX",
  ...
};

// You might not need this statement... just the firebase.initializeApp...
export default !firebase.apps.length
  ? firebase.initializeApp(firebaseConfig)
  : firebase.app();

You can find your firebaseConfig in your Project Settings.