#firebase connection
19 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
I am not quite sure if this should be like that (see attached). If undefined is fine, then provide more info
i don't know bro x) i never use nosql so it's new for me ..
look my config : ```js
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>
);
}```
You try to connect your client to the database. That does not work, because your env variables are only available serverside.
Two options now:
- Make the env variables public (watch out)
- Connect your client to the nextjs server (might not be supported)
did you added your .env variables to your hosting provider?
Hi @cerulean dagger
@buoyant pier opinion is right.
you can add NEXT_PUBLIC_ prefix to your environment variables.
yea, that's the 1. solution from here #1308103147750817902 message (<--- click)
Thanks for confirming this 👍
thx really ❤️
You are welcome.