Next.js Discord

Discord Forum

firebase connection

Answered
Mallow bee posted this in #help-forum
Open in Discord
Mallow beeOP
hi all, i learn next and i want use firebase for my project but i have this error (see picture)

i don't know how to solve this ... thx for your help
Answered by Sun bear
you can add NEXT_PUBLIC_ prefix to your environment variables.
View full answer

17 Replies

@Mallow bee hi all, i learn next and i want use firebase for my project but i have this error (see picture) i don't know how to solve this ... thx for your help
I am not quite sure if this should be like that (see attached). If undefined is fine, then provide more info
@B33fb0n3 I am not quite sure if this should be like that (see attached). If undefined is fine, then provide more info
Mallow beeOP
i don't know bro x) i never use nosql so it's new for me ..
look my config :
 
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    apiKey: process.env.FIREBASE_APIKEY,
    authDomain: process.env.FIREBASE_AUTHDOMAIN,
    projectId: process.env.FIREBASE_PROJECTID,
    storageBucket: process.env.FIREBASE_STORAGEBUCKET,
    messagingSenderId: process.env.FIREBASE_MESSAGING,
    appId: process.env.FIREBASE_APPID,
    measurementId: process.env.FIREBASE_MEASUREMENTID
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

export { app, db };
and the code :

"use client"
import { createContext, useContext, useEffect, useState } from "react";
import { collection, onSnapshot } from "firebase/firestore";
import { db } from "../db/firebaseConfig";
import { DataType, DbContextType } from "../Types/useType";

const UsersContext = createContext<DbContextType | null>(null);

export const useFirebase = () => {
    const context = useContext(UsersContext);
    if (!context) {
        throw new Error("useUsers must be used within a UsersProvider")
    }
    return context;
}

export const UsersProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
    const [users, setUsers] = useState<DataType[]>([]);

    useEffect(() => {
        const unsubscribe = onSnapshot(
            collection(db, "users"), 
            (snapshot) => {
                const data: DataType[] = snapshot.docs.map(doc => ({
                    id: doc.id,
                    ...doc.data()
                } as DataType));
                setUsers(data);
            },
            (error) => {
                console.error("Firestore snapshot error:", error);
            }
        );

        return () => unsubscribe();
    }, []);

    const value = { users };

    return (
        <UsersContext.Provider value={value}>
            {children}
        </UsersContext.Provider>
    );
}
Mallow beeOP
i'm on local, and i use .env.local
Mallow beeOP
ok without env local it work
but why ?
did you added your .env variables to your hosting provider?
@Mallow bee ok without env local it work
Sun bear
Hi @Mallow bee
@B33fb0n3 opinion is right.
Sun bear
you can add NEXT_PUBLIC_ prefix to your environment variables.
Answer
Mallow beeOP
oh
thx
@Sun bear you can add `NEXT_PUBLIC_` prefix to your environment variables.
Mallow beeOP
thx really ❤️
@Mallow bee thx really ❤️
Sun bear
You are welcome.