mau*_*ice 23 javascript import firebase reactjs next.js
我正在尝试在 Next.js (firebase v9) 中实现并导出 firebase 分析模块
对于以下代码片段,我收到错误“ReferenceError:窗口未定义” 。之前的所有功能都运行良好。
任何想法如何解决这一问题?
import { initializeApp, getApps, getApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getAuth } from 'firebase/auth'
import { getFirestore } from '@firebase/firestore'
const firebaseConfig = {
//...
};
// Initialize Firebase
const app = !getApps().length ? initializeApp(firebaseConfig) : getApp();
const auth = getAuth();
const db = getFirestore(app)
// try to add analytics
const analytics = getAnalytics(app)
export {auth, db, analytics}
Run Code Online (Sandbox Code Playgroud)
小智 22
下一个JS:
import {initializeApp} from 'firebase/app';
import {getFirestore} from 'firebase/firestore';
import {getAnalytics} from 'firebase/analytics';
const firebaseConfig = JSON.parse(
process?.env?.FIREBASE_CONFIG ?? '{}',
);
let analytics; let firestore;
if (firebaseConfig?.projectId) {
// Initialize Firebase
const app = initializeApp(firebaseConfig);
if (app.name && typeof window !== 'undefined') {
analytics = getAnalytics(app);
}
// Access Firebase services using shorthand notation
firestore = getFirestore();
}
export {analytics, firestore};
Run Code Online (Sandbox Code Playgroud)