下一页 身份验证火基后端"ReferenceError: Cannot access 'app' before initialization"



当使用下一个认证时,我试图将用户存储在firebase后端,我无法绕过此错误

使用下一个授权的FIREBASE适配器。

https://next-auth.js.org/adapters/firebase:DOCS IM FOLLOWING

重火力点客户

import firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASURMENT_ID,
};
// Initialize Firebase
const analytics = getAnalytics(app);
const app = initializeApp(firebaseConfig);
export default firebase;

[…nextauth] . js

import NextAuth from "next-auth";
import { FirebaseAdapter } from "@next-auth/firebase-adapter";
import firebaseClient from "../../../firebase/FirebaseClient";
import GoogleProvider from "next-auth/providers/google";
import firebase from "firebase/app";
import "firebase/firestore";
const firestore = (firebase.apps[0] ?? firebaseClient).firestore();
export default NextAuth({
// Configure one or more authentication providers
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
adapter: FirebaseAdapter(firestore),
});

犯错

ReferenceError: Cannot access 'app' before initialization

我打赌您需要像下面这样切换行,因为您在创建分析时没有app变量:

const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

备选问题:内部引用函数

我们的问题有点不同。如果您开发了一个具有内部引用函数的云函数,如下所示:

export.foo = functions.https.onCall(async () => { 
// Some functionality here that references internalFunc
const internalFunc = () => { ... }
}

internalFunc不能被引用。

我们的解决方案是在函数目录中创建一个utilities.js文件,然后从该文件中导出internalFunc。然后,将该函数导入到部署云功能的脚本中。

希望这能帮助到一些人!

相关内容

最新更新