javascript 未捕获的类型错误:无法读取未定义的属性“initializeApp”

zsohkypk  于 2023-05-05  发布在  Java
关注(0)|答案(5)|浏览(216)

我尝试在我的Vue项目中使用一些Firebase库(我使用npm install firebase安装了它)。
我在main.js中添加了这些:

import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'

Firebase.initializeApp({
  apiKey: 'xxx',
  authDomain: 'xxx',
  databaseURL: 'xxx',
  projectId: 'xxx',
  storageBucket: 'xxx',
  messagingSenderId: 'xxx',
  appId: 'xxx',
  measurementId: 'xxx'
})

我得到:
未捕获的类型错误:无法读取未定义的属性“initializeApp”

mdfafbf1

mdfafbf11#

更改此:

import { Firebase } from 'firebase/app'

变成这样:

import * as firebase from "firebase/app";

firebase/app导入所有内容,然后执行:

firebase.initializeApp({
  apiKey: 'xxx',
  authDomain: 'xxx',
  databaseURL: 'xxx',
  projectId: 'xxx',
  storageBucket: 'xxx',
  messagingSenderId: 'xxx',
  appId: 'xxx',
  measurementId: 'xxx'
})
8ehkhllq

8ehkhllq2#

对于使用最新**Firebase Modular API(v9)**的新手,需要进行重构。
导入必须更改为:

import { FirebaseApp, initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getDatabase } from "firebase/database";

const firebaseConfig = {...}

// Initialize Firebase
const app: FirebaseApp = initializeApp(firebaseConfig);

const db = getDatabase(app);

const auth = getAuth(app);

关于这个新的重构风格
有关读取和写入数据如何更改的更多信息,请单击此处

ve7v8dk2

ve7v8dk23#

检查package.json文件中的Firebase版本。如果是9+,应该是这样的:

import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';
import { getAuth } from "firebase/auth";

然后:

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

// Initialize Firebase
const firebase = initializeApp(firebaseConfig);
const db = getFirestore(firebase);
const auth = getAuth(firebase);
sxpgvts3

sxpgvts34#

由于您使用的版本与已安装的SDK版本不同而出现的错误
简单地从这样改变:

import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'

对此:

import { Firebase } from 'firebase/compat/app'
   import 'firebase/compat/analytics'
   import 'firebase/compat/auth'
   import 'firebase/compat/messaging'

有关更清晰的信息,您可以阅读此SDK9

fslejnso

fslejnso5#

TypeError: Cannot read properties of undefined (reading 'initializeApp')
 
**Please Follow Steps From** https://firebaseopensource.com/projects/rakannimer/react-firebase/

If Still Stuck on Same Issue It Must Be error Of Not Installing the sub packages
Required

npm i @react-firebase/database --force Install This. 

**my Code** 
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
    apiKey: "YOUR_API_KEY",
    authDomain: "YOUR_DOMAIN_KEY",
    databaseURL: "YOUR_DATABASE_URL",
    projectId: "YOUR_PROJECT_ID",
    storageBucket: "YOUR_STORAGE_BUCKET",
    messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
    appId: "YOUR_APP_ID",
    measurementId: "YOUR_MEASUREMENT_ID"
};

// Initialize Firebase`enter code here`
const app = initializeApp(firebaseConfig);

相关问题