最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I set up firebase analytics on my react native expo project? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jan 19 at 18:54 Doug Stevenson 317k36 gold badges454 silver badges472 bronze badges Recognized by Google Cloud Collective asked Jan 19 at 18:16 newbie codernewbie coder 8381 gold badge11 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论