简体   繁体   English

NextAuth Firebase 后端“ReferenceError:初始化前无法访问‘app’”

[英]NextAuth Firebase Backend "ReferenceError: Cannot access 'app' before initialization"

I'm trying to store users on a firebase backend when using next auth I can't get around this error我试图在使用 next auth 时将用户存储在 firebase 后端 我无法解决此错误

Using next-auth FIREBASE adapter.使用 next-auth FIREBASE 适配器。

https://next-auth.js.org/adapters/firebase :DOCS IM FOLLOWING https://next-auth.js.org/adapters/firebase :我正在关注文档

FIREBASE CLIENT FIREBASE 客户

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 [...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),
});

err

ReferenceError: Cannot access 'app' before initialization

I bet you need to switch lines as following, because you don't have app variable when you create analytics:我敢打赌,您需要按以下方式切换行,因为在创建分析时没有app变量:

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

Alternative Problem: Internally-referenced Functions备选问题:内部引用函数

Our issue was a bit different.我们的问题有点不同。 If you've developed a cloud function that has an internally-referenced function, like this:如果您开发了一个云 function,它有一个内部引用的 function,如下所示:

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

const internalFunc = () => { ... }
}

That internalFunc won't be able to be referenced.internalFunc将无法被引用。

The solution for us is to create a utilities.js file in our functions directory, then export the internalFunc from that file.我们的解决方案是在我们的函数目录中创建一个utilities.js文件,然后从该文件中导出internalFunc Then, import that function to the script where you're deploying the cloud function.然后,将该 function 导入到部署云 function 的脚本中。

Hope this helps someone!希望这对某人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FIREBASE - ReferenceError:初始化前无法访问“auth” - FIREBASE - ReferenceError: Cannot access 'auth' before initialization unhandledRejection:ReferenceError:初始化前无法访问“firestore” - unhandledRejection: ReferenceError: Cannot access 'firestore' before initialization ReferenceError:在初始化之前无法访问“数学” - ReferenceError: Cannot access 'Math' before initialization 未捕获的 ReferenceError:在初始化之前无法访问“类” - Uncaught ReferenceError: Cannot access 'Class' before initialization ReferenceError:初始化前无法访问模块 - ReferenceError: Cannot access module before initialization ReferenceError:在初始化之前无法访问“mockMethod1” - ReferenceError: Cannot access 'mockMethod1' before initialization ReferenceError:无法在初始化之前访问“UserContext” - React - ReferenceError: Cannot access 'UserContext' before initialization - React 未捕获的 ReferenceError:初始化前无法访问“CONFIG” - Uncaught ReferenceError: Cannot access 'CONFIG' before initialization ReferenceError:初始化前无法访问“播放器” - ReferenceError: Cannot access 'Player' before initialization 开玩笑 ReferenceError:初始化前无法访问“” - jest ReferenceError: Cannot access '' before initialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM