如何通过反应从Firestore获取登录用户的数据?

how to get data of logged in users from Firestore with react?

嘿伙计们,我想使用 react 和 firebase v9 从 Firestore 中登录用户数据,但在控制台中我正在获取所有用户的数据
这是我的代码:

const usersCollectionRef = collection(db, "users");

useEffect(() => {
   onAuthStateChanged(auth, (user) => {
      if (user) {
        getDocs(usersCollectionRef, user.uid).then((snapshot) => {
          console.log(snapshot);
        });
      }
    });

  }, []);

我只想获取登录用户的数据。

无需获取用户集合中的所有文档,只要知道文档 ID,就可以获取单个文档。您可以使用 doc() to create a DocumentReference to that user's document and then use getDoc() 来获取:

useEffect(() => {
  onAuthStateChanged(auth, async (user) => {
    if (user) {
      const snapshot = await getDoc(doc(db, "users", user.uid))
      console.log(snapshot.data())
    }
  });
}, []);