javascript 如何在注册后将用户凭据存储在firestore中

qoefvg9y  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(107)

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));
flvlnr44

flvlnr441#

我通过将collection替换为doc来解决此问题
首先确保从firebase/firebase导入doc,如下所示

import { doc } from "firebase/firestore"

然后更改此项

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

结束日期

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

相关问题