简体   繁体   English

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

[英]Uncaught TypeError: Cannot read property 'initializeApp' of undefined

I'm trying to use a few Firebase libraries in my Vue project (I installed it using npm install firebase ).我正在尝试在我的 Vue 项目中使用一些 Firebase 库(我使用npm install firebase安装它)。

I added those in main.js :我在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'
})

And I get:我得到:

Uncaught TypeError: Cannot read property 'initializeApp' of undefined未捕获的类型错误:无法读取未定义的属性“initializeApp”

Change this:改变这个:

import { Firebase } from 'firebase/app'

into this:进入这个:

import * as firebase from "firebase/app";

import everything from firebase/app then do:firebase/app导入所有内容,然后执行以下操作:

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

Check your Firebase version in your package.json file.package.json文件中检查 Firebase 版本。 If it is 9+ it should be something like this:如果它是 9+,它应该是这样的:

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

and then:接着:

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

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

Change this:改变这个:

import { Firebase } from 'firebase/app'

into this:进入这个:

import * as firebase from "firebase/app";

import everything from firebase/app then do:从 firebase/app 导入所有内容,然后执行:

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

For newcomers using the newest Firebase Modular API (v9) a refactoring is required.对于使用最新Firebase Modular API (v9)的新手,需要进行重构。

The imports must be changed to this:导入必须更改为:

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);

About this new refactoring style here关于这种新的重构风格在这里

And more information on how Read and Write data changed here有关读取和写入数据如何更改的更多信息,请点击此处

Error that appears caused by the difference of the version you use with the installed SDK version您使用的版本与安装的SDK版本不同导致出现的错误

Simply change from like this:只需像这样更改:

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

to this:对此:

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

For clearer info, you can read this SDK9如需更清晰的信息,您可以阅读此SDK9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“id” - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'id' of undefined Firebase recaptchaVerifier 显示未捕获的类型错误:无法读取未定义的属性“RecaptchaVerifier” - Firebase recaptchaVerifier showing Uncaught TypeError: Cannot read property 'RecaptchaVerifier' of undefined 类型错误:无法读取第 100 行未定义的属性“4” - TypeError: Cannot read property '4' of undefined at Line 100 类型错误:无法读取未定义的属性“长度”- Firebase - TypeError: Cannot read property 'length' of undefined - Firebase TypeError:无法使用 nodejs 读取未定义的属性“byteLength” - TypeError: Cannot read property 'byteLength' of undefined with nodejs 未捕获的类型错误:无法解构“未定义”的属性“getFirestore”,因为它是未定义的 - Uncaught TypeError: Cannot destructure property 'getFirestore' of 'undefined' as it is undefined 类型错误:无法读取未定义的属性“_fieldsProto” - TypeError: Cannot read property '_fieldsProto' of undefined 类型错误:无法读取未定义的属性“ref” - TypeError: Cannot read property 'ref' of undefined 类型错误:无法读取未定义的属性“数字” - TypeError: Cannot read property 'Digits' of undefined Firebase,类型错误:无法读取未定义的属性“错误” - Firebase, TypeError: Cannot read property 'error' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM