Is there any way to store an user inside firebase database but with custom document name. What I am trying to achieve is that after the user signs up to the app, the app saves their details into a firebase database.
Here is my code:
function signUp(email, password) {
return createUserWithEmailAndPassword(auth, email, password).then(() => {
addDoc(usersCollectionRef, auth.currentUser.uid,{
email: email,
password: password,
id: auth.currentUser.uid,
role: "user",
});
});
}
As you can see I'm trying to save the document name like this: auth.currentUser.uid
, which I put as the second parameter of addDoc
function, but I get this error:
Function addDoc() called with invalid data. Data must be an object, but it was: "B0P4tBzY2uOF2XG2o6cT..." (found in document users/Gmbv7Zd2JZF4PtTct4VF)
Does anyone know how to do this?
CodePudding user response:
addDoc will always generate a new random document ID. If you want to specify your own document ID, don't use addDoc. Instead, follow the instructions in the documentation and build a reference to the document to create, then create it with setDoc:
import { doc, setDoc } from "firebase/firestore";
setDoc(doc(usersCollectionRef, auth.currentUser.uid), {
email: email,
password: password,
id: auth.currentUser.uid,
role: "user",
});