I followed this documentation which seems pretty straightforward, however I keep getting error while setting analytics user id: [Error: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()]
This is part of my App.js
file:
import { auth } from './firebase.js'
import analytics from '@react-native-firebase/analytics';
const App = () => {
const [currentUser, setCurrentUser] = useState(null);
useEffect(() => {
(async () => {
if(currentUser){
console.log("I have current user: ", analytics())
try{
await analytics().setUserId(currentUser.uid)
console.log("all done")
}catch(error){
console.log("error while setting analytics user id: ", error);
}
}
})();
}, [currentUser]);
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
if (user) {
setCurrentUser(user);
}
});
return () => unsubscribe();
}, []);
if(!currentUser){
return (
<NavigationContainer>
<AuthNavigator />
</NavigationContainer>
);
}
else{
return (
<NavigationContainer>
<MainNavigator />
</NavigationContainer>
);
}
};
export default App;
My firebase.js file:
import firebase from "firebase/compat/app";
import 'firebase/compat/firestore'
import 'firebase/compat/auth'
const firebaseConfig = {
apiKey: "some_key",
authDomain: "some_domain",
databaseURL: "some_url",
projectId: "some_project_id",
storageBucket: "some_id",
messagingSenderId: "id",
appId: "id",
measurementId: "id"
};
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
const authState = firebase.auth();
export const auth = authState;
export const firestore = firebase.firestore();
Can someone please help because I spent too much time on this issue. I followed the docs correctly, I dont understand why it is complaining for [Error: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()]
Please dont point me to the docs, I read them multiple times, I need practical solution.
Your firebase.js is using the standard firebase
package for initialization, but your App.js is using @react-native-firebase
. These packages are not compatible with each other. You can't initialize Firebase using the firebase
package and have it work with @react-native-firebase
APIs.
You said you don't want a link to documentation, but I'll encourage you to reconsider that maybe you read the wrong documentation as far as initialzing Firebase is concerned. If you're using @react-native-firebase
for Analytics, then you should init Firebase using the correct documentation. On that page, there are instructions for initializing Firebase in an Expo project. I won't bother copying all of that here, but it sounds like you simply need to add some config plugins to make it work:
The recommended approach to configure React Native Firebase is to use Expo Config Plugins. You will add React Native Firebase modules to the plugins array of your app.json or app.config.js. See the note below to determine which modules require Config Plugin configurations.