Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase store an user into database after signup with a custom document name (Firebase v9)

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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",
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading