Firebase 配置 2022

发布于 2025-01-11 15:44:22 字数 2617 浏览 0 评论 0原文

我正在编写 FB 克隆教程。问题是我是在 2022 年做的,所以需要实施很多更新。我对 firebase 很陌生,所以我确实需要一些帮助。

我的 firebase.js

import firebase from 'firebase';
import 'firebase/storage';

// Your web app's Firebase configuration
const firebaseConfig = {
    apiKey: "AIzaSyDNCuXEn5todyFVWl6CLJaNridOkTxLi1o",
    authDomain: "fbclone-b8dca.firebaseapp.com",
    projectId: "fbclone-b8dca",
    storageBucket: "fbclone-b8dca.appspot.com",
    messagingSenderId: "366781998977",
    appId: "1:366781998977:web:84791acf7d270c5fdbaa80"
};

// // Initialize Firebase
// const app = initializeApp(firebaseConfig);

const app = !fireBase.apps.length ? fireBase.initializeApp(firebaseConfig) : fireBase.app;

const db = app.firestore();

const storage = fireBase.storage();

export {db, storage}

我的 inputbox.js

import { useSession } from "next-auth/react"
import Image from "next/image"
import { CameraIcon, EmojiHappyIcon, VideoCameraIcon } from "@heroicons/react/solid";
import {useRef} from 'react'
import {db, storage} from '../fireBase';

const InputBox = () => {
    const {data: session} = useSession();
    const inputRef = useRef(null);

    const sendPost = (e) => {
        e.preventDefault();
        if (!inputRef.current.value) return;

        db.collection("posts").add({
                message: inputRef.current.value,
                name: session.user.name,
                email: session.user.email,
                image: session.user.image,
                timestamp: firebase.firestore.FieldValue.serverTimestamp(),
            })

        inputRef.current.value = ""
    }

这是我得到的错误:

./fireBase.js:1:0
Module not found: Package path . is not exported from package /home/alondrob/code/labs/projects/fbclone/node_modules/firebase (see exports field in /home/alondrob/code/labs/projects/fbclone/node_modules/firebase/package.json)
> 1 | import firebase from 'firebase';
  2 | import 'firebase/storage';
  3 | 
  4 | // Your web app's Firebase configuration

Import trace for requested module:
./components/InputBox.js
./components/Feed.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found

我知道导入语法已更改,并且我尝试了一些操作,例如

// Initialize Cloud Firestore through Firebase
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

const db = getFirestore();

我只是不确定如何将其全部配置在一起。

I am coding along on a FB clone tutorial. The issue is that I'm doing it in 2022, so a lot of updates need to be implemented. I'm really new to firebase, so i do need some help on this.

my firebase.js

import firebase from 'firebase';
import 'firebase/storage';

// Your web app's Firebase configuration
const firebaseConfig = {
    apiKey: "AIzaSyDNCuXEn5todyFVWl6CLJaNridOkTxLi1o",
    authDomain: "fbclone-b8dca.firebaseapp.com",
    projectId: "fbclone-b8dca",
    storageBucket: "fbclone-b8dca.appspot.com",
    messagingSenderId: "366781998977",
    appId: "1:366781998977:web:84791acf7d270c5fdbaa80"
};

// // Initialize Firebase
// const app = initializeApp(firebaseConfig);

const app = !fireBase.apps.length ? fireBase.initializeApp(firebaseConfig) : fireBase.app;

const db = app.firestore();

const storage = fireBase.storage();

export {db, storage}

my inputbox.js

import { useSession } from "next-auth/react"
import Image from "next/image"
import { CameraIcon, EmojiHappyIcon, VideoCameraIcon } from "@heroicons/react/solid";
import {useRef} from 'react'
import {db, storage} from '../fireBase';

const InputBox = () => {
    const {data: session} = useSession();
    const inputRef = useRef(null);

    const sendPost = (e) => {
        e.preventDefault();
        if (!inputRef.current.value) return;

        db.collection("posts").add({
                message: inputRef.current.value,
                name: session.user.name,
                email: session.user.email,
                image: session.user.image,
                timestamp: firebase.firestore.FieldValue.serverTimestamp(),
            })

        inputRef.current.value = ""
    }

this is the error that i get:

./fireBase.js:1:0
Module not found: Package path . is not exported from package /home/alondrob/code/labs/projects/fbclone/node_modules/firebase (see exports field in /home/alondrob/code/labs/projects/fbclone/node_modules/firebase/package.json)
> 1 | import firebase from 'firebase';
  2 | import 'firebase/storage';
  3 | 
  4 | // Your web app's Firebase configuration

Import trace for requested module:
./components/InputBox.js
./components/Feed.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found

I know the import syntax has changed and i tried few things like

// Initialize Cloud Firestore through Firebase
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

const db = getFirestore();

I just not sure how to configure it all together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2025-01-18 15:44:22

与其从过时的教程开始,不如考虑从 Firebase 文档代码实验室。他们完全了解最新的变化。

虽然最新的 SDK 可能具有不同的语法,但旧的 SDK 仍然可以正常工作。因此,如果您使用过时的教程/资源,您始终可以使用该教程所基于的 SDK 版本。您还可以在兼容模式下使用最新的 SDK,具体方法如下:升级指南中的说明。

Instead of starting from an outdated tutorial, consider starting from the Firebase documentation or codelab. They are fully up to date with the latest changes.

While the latest SDKs may have a different syntax, the older SDKs continue to work just fine. So if you work from an outdated tutorial/resource, you can always use the SDK version that the tutorial is based on. You can also use the latest SDK in compat mode, by following the instructions in the upgrade guide.

寒尘 2025-01-18 15:44:22

您正在处理 firebase 版本 8 代码库,并尝试将本地 firbase.js 组件导入 inbox.js 组件

问题:您必须安装 firebase,但未将版本指定为 版本 8, 这意味着您安装了最新版本的 firebase,版本 9 而不是版本8.

因此,您必须重构 firebase.js 组件内的代码,如下所示。

 import { initializeApp, getApps, getApp } from "firebase/app";
    import { getFirestore } from "firebase/firestore";
    import { getStorage } from "firebase/storage";
    
    const firebaseConfig = {
      apiKey: "AIzaSyDNCuXEn5todyFVWl6CLJaNridOkTxLi1o",
      authDomain: "fbclone-b8dca.firebaseapp.com",
      projectId: "fbclone-b8dca",
      storageBucket: "fbclone-b8dca.appspot.com",
      messagingSenderId: "366781998977",
      appId: "1:366781998977:web:84791acf7d270c5fdbaa80"
    };
    
   
    const app = !getApps().length ? initializeApp(firebaseConfig) : 
    getApp();
    
    const db = getFirestore(app);
    const storage = getStorage(app);
    
export default {db, storage};

尽管上述解决方案可以解决您的问题,但 inputbox.js 还会产生另一个问题。
因此,在 inputbox.js 组件代码中,重构如何从 firebase 获取数据 点击此链接进行

重构

db.collection("posts").add({
                message: inputRef.current.value,
                name: session.user.name,
                email: session.user.email,
                image: session.user.image,
                timestamp: firebase.firestore.FieldValue.serverTimestamp(),
            })

Your are dealing with firebase version 8 code base and trying to import your local firbase.js component inside inbox.js component

Issue: you must have install firebase without specifying the version as version 8, which means you have install the latest version of firebase, version 9 instead of version 8.

As such you must refactor the code inside firebase.js component as below.

 import { initializeApp, getApps, getApp } from "firebase/app";
    import { getFirestore } from "firebase/firestore";
    import { getStorage } from "firebase/storage";
    
    const firebaseConfig = {
      apiKey: "AIzaSyDNCuXEn5todyFVWl6CLJaNridOkTxLi1o",
      authDomain: "fbclone-b8dca.firebaseapp.com",
      projectId: "fbclone-b8dca",
      storageBucket: "fbclone-b8dca.appspot.com",
      messagingSenderId: "366781998977",
      appId: "1:366781998977:web:84791acf7d270c5fdbaa80"
    };
    
   
    const app = !getApps().length ? initializeApp(firebaseConfig) : 
    getApp();
    
    const db = getFirestore(app);
    const storage = getStorage(app);
    
export default {db, storage};

Though, the solution above will solve your issue but one more will result from inputbox.js.
So inside inputbox.js component code, refactor how you are fetchig data from firebase follow this link for that

Refactor this

db.collection("posts").add({
                message: inputRef.current.value,
                name: session.user.name,
                email: session.user.email,
                image: session.user.image,
                timestamp: firebase.firestore.FieldValue.serverTimestamp(),
            })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文