简体   繁体   中英

TypeError: Cannot read property 'length' of undefined - Firebase

I am trying to connect to Firebase.

Code from firebase.js:

import * as firebase from "firebase/compat/app";

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

const app = !firebase.apps.length
  ? firebase.initializeApp(firebaseConfig)
  : firebase.app();

const db = app.firestore();
const auth = app.auth();
const provider = new firebase.auth.GoogleAuthProvider();

export { db, auth, provider };

错误信息

As I said in my comment on your other thread, firebase imports have changed recently. Try to change fribase to firebase/compat/app Documentation

You're missing imports for Firestore (as the error mentions) and Authentication.

import * as firebase from "firebase/compat/app";
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

Also see the documentation on updating imports to v9 compat .

Just coming across this, I hope this solution helps:
simply upgrade "firebase" to version: "^9.4.0"

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { getAuth, GoogleAuthProvider } from 'firebase/auth'

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

const app = initializeApp(firebaseConfig);

const db = getFirestore(app);//access to the database
const auth = getAuth(); //access to the authentication
const provider = new GoogleAuthProvider(); //access tp the provider

export { db, auth, provider };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM