如何将其转换为异步函数?

发布于 2025-02-05 16:25:34 字数 1064 浏览 2 评论 0原文

我希望能够从Firestore数据库中检索用户,并过滤以找到使用数据库中用户ID的当前记录的ID之间的匹配。我无法做到这一点,因为我无法找到一种将其更改为async函数的方法:

const [loggedUser, setLoggedUser] = useState([]);
const [data, setData] = useState([]);

  useEffect(() => {
    const getUserData =  () => {
       onSnapshot(collection(db, "users"), (snapshot) => {
        let list = [];
          snapshot.docs.forEach((doc) => {
          list.push({ id: doc.id, ...doc.data() });
          setData(list);
        });

      }, (err) => {
        console.log(err);
      });

    }

    getUserData();


  }, [])

  useEffect(() => {

    const getLoggedUser = onAuthStateChanged(auth, (user) => {
      if (user) {
        const uid = user.uid;
        console.log(uid);
        if (data) {
          const signedUser = data.filter((item) => item.id === uid);
          setLoggedUser(signedUser);
        } else {
          console.log("no matching data")
        }
      } else {
        console.log("no user found")

      }
    });

    getLoggedUser();
  }, [])

I want to be able to retrieve the users from the Firestore database and filter to find a match between the id of the current logged in user with the id of the user from the database. I am not able to do that because I can't figure out a way to change this to async function:

const [loggedUser, setLoggedUser] = useState([]);
const [data, setData] = useState([]);

  useEffect(() => {
    const getUserData =  () => {
       onSnapshot(collection(db, "users"), (snapshot) => {
        let list = [];
          snapshot.docs.forEach((doc) => {
          list.push({ id: doc.id, ...doc.data() });
          setData(list);
        });

      }, (err) => {
        console.log(err);
      });

    }

    getUserData();


  }, [])

  useEffect(() => {

    const getLoggedUser = onAuthStateChanged(auth, (user) => {
      if (user) {
        const uid = user.uid;
        console.log(uid);
        if (data) {
          const signedUser = data.filter((item) => item.id === uid);
          setLoggedUser(signedUser);
        } else {
          console.log("no matching data")
        }
      } else {
        console.log("no user found")

      }
    });

    getLoggedUser();
  }, [])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2025-02-12 16:25:34

我希望能够从firestore数据库中检索用户并过滤以找到使用数据库中用户ID登录的当前ID之间的匹配。

您可以使用 >相反,这只会获取用户的文档,只需花费1个读取。当前,您正在阅读整个集合,这些集合将花费您n读,其中n是用户集合中的文档数量。

您可以仅使用usefeft()一次,然后在更新验证状态后查询firestore。尝试重构代码,如下所示:

import { getDoc, doc } from "firebase/firestore"

const [loggedUser, setLoggedUser] = useState([]);
const [data, setData] = useState([]);


useEffect(() => {
  onAuthStateChanged(auth, (user) => {
    if (user) {
      const uid = user.uid;
      console.log("User UID:", uid);
       
      const snapshot = await getDoc(doc(db, "users", uid));
    
      if (snapshot.exists) {
        setLoggedUser(snapshot.data());
      } else {
        console.log("user document missing")
      }
    } else {
      console.log("User not logged in")
    }
  });
}, [])

I want to be able to retrieve the users from the Firestore database and filter to find a match between the id of the current logged in user with the id of the user from the database.

You can use getDoc instead that'll only fetch the user's document and will cost you only 1 read. Currently you are reading the whole collection that'll cost you N reads where N is number of documents in the users collection.

You can use useEffect() just once and query Firestore when the auth state has been updated. Try refactoring the code as shown below:

import { getDoc, doc } from "firebase/firestore"

const [loggedUser, setLoggedUser] = useState([]);
const [data, setData] = useState([]);


useEffect(() => {
  onAuthStateChanged(auth, (user) => {
    if (user) {
      const uid = user.uid;
      console.log("User UID:", uid);
       
      const snapshot = await getDoc(doc(db, "users", uid));
    
      if (snapshot.exists) {
        setLoggedUser(snapshot.data());
      } else {
        console.log("user document missing")
      }
    } else {
      console.log("User not logged in")
    }
  });
}, [])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文