ReferenceError: window is not defined
嘗試在 Next.js 專案中初始化 Firebase 分析時出現錯誤。
這是相關的代碼片段:
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
let analytics;
if (window !== undefined) {
analytics = getAnalytics(app);
}
onAuthStateChanged(auth, (user) => {
if (user) {
// User signed in
const uid = user.uid;
console.log(uid);
} else {
// User signed out.
}
});
export { app, auth, analytics };
該問題已在另一篇文章中解決;但建議的解決方案似乎對我不起作用。
uj5u.com熱心網友回復:
如果window
不存在,則無法檢查其型別。
考慮:
if (typeof window !== 'undefined') {
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507500.html
標籤:Google Cloud Collective javascript 火力基地 下一个.js 谷歌分析