简体   繁体   English

FirebaseError:firebase.initializeApp (NextJS) 中未提供“projectId”

[英]FirebaseError: "projectId" not provided in firebase.initializeApp (NextJS)

I'm building a web app with NextJS, NextAuth and Firebase/Firestore, and I'm getting an error:我正在使用 NextJS、NextAuth 和 Firebase/Firestore 构建一个 web 应用程序,但出现错误:

error - [FirebaseError: "projectId" not provided in firebase.initializeApp.] { code: 'invalid-argument', customData: undefined, toString: [Function (anonymous)]错误 - [FirebaseError: firebase.initializeApp 中未提供“projectId”。] { code: 'invalid-argument', customData: undefined, toString: [Function (anonymous)]

This is my JS file:这是我的 JS 文件:

import NextAuth from "next-auth/next";
import TwitterProvider from "next-auth/providers/twitter";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";

import { initializeApp, getApp, getApps } from "firebase/app";
import "firebase/auth";
import { getFirestore, collection, addDoc, getDocs } from "firebase/firestore";
// import { getAnalytics } from "firebase/analytics";
// import { getStorage } from "firebase/storage";

import nextConfig from "next.config";

const firebaseConfig = {
  apiKey: nextConfig.env.FIREBASE_API_KEY,
  authDomain: nextConfig.env.FIREBASE_AUTH_DOMAIN,
  projectId: nextConfig.env.FIREBASE_PROJECT_ID,
  storageBucket: nextConfig.env.FIREBASE_STORAGE_BUCKET,
  messagingSenderId: nextConfig.env.FIREBASE_MESSAGING_SENDER_ID,
  appId: nextConfig.env.FIREBASE_APP_ID,
  measurementId: nextConfig.env.FIREBASE_MEASUREMENT_ID,
};

const app = !getApps().length ? initializeApp(firebaseConfig) : getApp();
//const analytics = getAnalytics(app);
const db = getFirestore(app);
//const storage = getStorage(app);

const dbInstance = collection(db, "bugs");

const getBugs = () => {
  getDocs(dbInstance).then((data) => {
    console.log(data);
  });
};

export default NextAuth({
  providers: [
    TwitterProvider({
      clientId: nextConfig.env.TWITTER_CLIENT_ID,
      clientSecret: nextConfig.env.TWITTER_CLIENT_SECRET,
      version: "2.0", // opt-in to Twitter OAuth 2.0
    }),
  ],
  adapter: FirestoreAdapter(db),
});

I can't find any solution on the inte.net.我在 inte.net 上找不到任何解决方案。

Any idea?任何的想法?

In Nextjs, environment variables are only available in the Node.js environment by default, meaning they won't be exposed to the browser.在 Nextjs 中,环境变量默认仅在 Node.js 环境中可用,这意味着它们不会暴露给浏览器。

In order to expose a variable to the browser you have to prefix the variable with NEXT_PUBLIC_为了向浏览器公开变量,您必须在变量前加上NEXT_PUBLIC_前缀

Change your firebase config as shown below and also change the firebase env variables in the .env file to use NEXT_PUBLIC_ prefix.如下所示更改您的 firebase 配置,并更改.env文件中的 firebase env 变量以使用NEXT_PUBLIC_前缀。

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_MEASUREMENT_ID,
};

暂无
暂无

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

相关问题 类型错误:firebase.initializeApp 不是 function - TypeError: firebase.initializeApp is not a function 没有创建 Firebase App - 调用 Firebase.initializeApp() - No Firebase App has been created - call Firebase.initializeApp() Firebase.initializeApp名称参数在移动端需要,web不需要 - Firebase.initializeApp name parameter is needed in mobile but not in web 在 WidgetsFlutterBinding.ensureInitialized() 中给出异常; 等待 Firebase.initializeApp(); - giving exception in WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); 没有创建 Firebase App '[DEFAULT]' - 在 Flutter 和 Firebase 调用 Firebase.initializeApp() - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase 依赖于 firebase 的根提供程序在 Firebase.initializeApp() 完成之前获取值 - Provider at root dependent on firebase fetches value before Firebase.initializeApp() completes 没有创建 Firebase App '[DEFAULT]' - 尽管正在初始化 Firebase,但调用 Firebase.initializeApp()) - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()) although Firebase is being initialized 没有 Firebase 应用程序“[DEFAULT]”已创建 - 在 Flutter 中调用 Firebase.initializeApp() - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter React Native 错误:没有创建 Firebase App '[DEFAULT]' - 调用 firebase.initializeApp() - React Native Error: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp() 没有创建 Firebase App '[DEFAULT]' - 调用 Firebase.initializeApp() -Flutter - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() -Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM