I'm learning these times how to work with Firebase and react Now, I created a "users" collection and add a test doc to it named "one" as I show below. However, When I tried to get this doc I get many data which probably has nothing to do with that doc.
Here I create the doc and take data from it
import { createContext } from "react";
import { db } from "../firebase";
const DbContext = createContext();
export const useDb = () => {
return useContext(DbContext);
};
export const DbProvider = ({ children }) => {
//creating "one" doc
const uploadToDb = (
email,
photo,
userName,
gender,
age,
phone,
country,
city
) => {
return db.collection("users").doc("one").set({
photo,
userName,
gender,
age,
phone,
country,
city,
});
};
//get data from doc
const getFromDb = (email) => {
var dbRef = db.collection("users").doc("one");
return dbRef.get();
};
const value = { uploadToDb, getFromDb };
return <DbContext.Provider value={value}>{children}</DbContext.Provider>;
};
Here I'm trying to get and print the data Im getting
useEffect(() => {
const getDetails = async () => {
try {
const res = await getFromDb(currentUser.email);
console.log("res", res);
} catch (error) {
console.error(error);
}
};
getDetails();
}, []);
"DB" is the name of firebase/firestore which I export from firebase.js file