How to store user credential in firestore after they have signup

Hi can you please help me I am trying to store user credential in firestore after they have signup but I keep getting this error

Invalid collection reference. Collection references must have an odd number of segments, but userDatabase/QMJiHnhoCchs6MKmPFk7wzTrlau1 has 2.

below is my code Many thank

const db = getFirestore(app)

//Sign up User Method

createUserWithEmailAndPassword( auth, email, password)
  .then((res) => {
    // Signed in 

   const uid = res.user.uid;

    const data = {
      id:uid,
      email,
       name,
  };



  const ref = collection(db, 'userDatabase',uid)

  console.log(ref);
  setDoc(ref, data)
      .then(() => console.log("Created New User Document Successfully"))
      .catch((e) => console.log("Error" , e));

).catch((e)=>console.log(e))

After I commented all the line from the createUserWithEmailAndPassword I notice that this

const ref = collection(db, 'userDatabase',uid)

line was the one generating the error but I can't just figure out why I have tried several methods to resolve this issues which have not work.

createUserWithEmailAndPassword(auth, email, password)
  .then((res) => {
    // Signed in 
    const uid = res.user.uid;

   // const data = {
   //   id:uid,
   //   email,
    //   name,
  //};



  const ref = collection(db, 'userDatabase',uid)

  console.log(ref);
//setDoc(ref, data)
   //   .then(() => console.log("Created New User Document Successfully"))
     // .catch((e) => console.log("Error" , e));
    

Upvotes: 0

Views: 78

Answers (1)

I walk around this issue by replacing the collection with doc

first make sure you import doc from firebase/firebase like so

import { doc } from "firebase/firestore"

then Change this

const ref = collection(db, 'userDatabase',uid)

To

const ref = doc(db, 'userDatabase',uid)

Upvotes: 1

Related Questions