Firebase Modular SDK V9.0.0+ TypeError:无法读取未定义 Firebase 的属性“应用程序" [英] Firebase Modular SDK V9.0.0+ TypeError: Cannot read property 'apps' of undefined firebase

查看:23
本文介绍了Firebase Modular SDK V9.0.0+ TypeError:无法读取未定义 Firebase 的属性“应用程序"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行我的 next.js 应用程序时出现此错误.我尝试了很多方法,但我无法解决这个问题.我正在使用 Firebase 9.0.1

I got this error when trying to run my next.js app. I try lots of ways but I can't solve this. I am using firebase 9.0.1

Server Error
TypeError: Cannot read property 'apps' of undefined

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
.nextserverpages\_app.js (14:13) @ Object../firebase.js

  12 | };
  13 |
> 14 | const app = !firebase.apps.length
     |             ^
  15 |   ? firebase.initializeApp(firebaseConfig)
  16 |   : firebase.app();

这是我的 firebase.js

Here is my firebase.js

import firebase from "firebase/app";

// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

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

推荐答案

由于您使用的是新的 Modular SDK v9.0.1,它不使用 firebase. 命名空间,你应该使用 getApps() 而不是 firebase.apps.

Since you are using the new Modular SDK v9.0.1 which does not use firebase. namespace, you should use getApps() instead of firebase.apps.

import { initializeApp, getApps } from "firebase/app"
import { getFirestore } from "firebase/firestore"
import { getAuth } from "firebase/auth"

const firebaseConfig = {...}

if (!getApps().length) {
  //....
}

const app = initializeApp(firebaseConfig)

const db = getFirestore(app)
const auth = getAuth(app)

export {db, auth}

不过,您无需检查 Firebase 是否已在此新 SDK 中初始化.您可以在文档中了解有关新语法的更多信息.

However, you don't need to check if Firebase is already initialized in this new SDK. You can learn more about the new syntax in the documentation.

同时查看:网络版 Firebase 入门

这篇关于Firebase Modular SDK V9.0.0+ TypeError:无法读取未定义 Firebase 的属性“应用程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆