在 Firestore 中,如何确定新创建的用户的 UID 并将该 UID 设置为文档名称?

In Firestore, how do I determine the UID of the newly created user and set that UID as the document name?

目前,我使用电子邮件作为文档名称,但我想改用 UID。我尝试使用“firebase.auth().currentUser.uid”,但它会从最后一个用户那里检索 UID。

try {
      setError("");
      setLoading(true);
      await signup(emailRef.current.value, passwordRef.current.value);
      await db
        .collection("users")
        .doc(emailRef)
        .set(
          {
            firstName: firstNameRef.current.value,
            lastName: lastNameRef.current.value,
          },
          { merge: true }
        )

我不确定 function signup() 和变量 emailRef,但您可以试试这个。 您可以从 userCredential 对象中获取新创建的用户的 UID。

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(async (userCredential) => {
    // Signed in 
    const user = userCredential.user;
    await db.collection("users").doc(user.uid).set({
      firstName: firstNameRef.current.value,
      lastName: lastNameRef.current.value,
    }, { merge: true })
    // ...
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });