Home > Mobile >  Cannot read properties of undefined (reading 'firestore')
Cannot read properties of undefined (reading 'firestore')

Time:05-25

i was trying to import my firebase project to expo, but got the error "firebase.js:15 Uncaught TypeError: Cannot read properties of undefined (reading 'firestore')". I have my firestore app like this:

import firebase, { initializeApp } from "firebase/app";
import "firebase/firestore";

const firebaseConfig = {
  apiKey: "xxx",
  authDomain: "xxx",
  projectId: "xxx",
  storageBucket: "xxx",
  messagingSenderId: "xxx",
  appId: "xxx",
};

initializeApp(firebaseConfig);

const db = firebase.firestore();

export { db };

Also import the initializeapp from firebase, otherwise the error is "firebase.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'initializeApp')"

The version of firebase I'm using is "firebase": "^9.8.1",

CodePudding user response:

For me when I used Expo I found I had to use the path:

firebase/compat/app

firebase/compat/firestore

instead, so like this;

import firebase from "firebase/compat/app";
import "firebase/compat/firestore";

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
};

const app = firebase.initializeApp(firebaseConfig);

export const db = app.firestore();

Also I'd recommend the slightly different way of declaring it as shown above, declaring the app constant because it's just nicer in case you need to add more exports such as:

const app = firebase.initializeApp(firebaseConfig);

export const auth = app.auth();
export const db = app.firestore();
export const storage = app.storage();

Give it a try and see if it works

  • Related