Firebase Modular SDK V9.0.0+ TypeError:无法读取未定义的 firebase 属性“apps”

soy*_*910 8 javascript firebase next.js

我在尝试运行 next.js 应用程序时遇到此错误。我尝试了很多方法但无法解决这个问题。我正在使用 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
.next\server\pages\_app.js (14:13) @ Object../firebase.js

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

这是我的 firebase.js

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
.next\server\pages\_app.js (14:13) @ Object../firebase.js

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

Dha*_*raj 17

v9.0.1由于您使用的是不使用命名空间的新模块化 SDK ,因此firebase.您应该使用.getApps()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}
Run Code Online (Sandbox Code Playgroud)

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

另请检查:Firebase 网络版入门