简体   繁体   English

如何在 reactjs 中获取 id

[英]How to get id in reactjs

  const dbRef = ref(getDatabase());

  useEffect(() => {
    getUser()

  }, [])
  

  const getUser = async () => {

    await get(child(dbRef, `AllUsers/${id}`)).then((snapshot) => {
      if (snapshot.exists()) {
        // setUser(...snapshot.val());
        console.log(snapshot)
      } else {
        // console.log("No data available");
        setUser({});
      }
    })

  }

在此处输入图像描述

If you want to log the value of the snapshot that you get back from the database, use its val() method:如果要记录从数据库中返回的快照的值,请使用其val()方法:

get(child(dbRef, `AllUsers/${id}`)).then((snapshot) => {
  if (snapshot.exists()) {
    console.log(snapshot.val());
  } else {
    // console.log("No data available");
    setUser({});
  }
})

I also recommend not combining await and then .我还建议不要组合awaitthen Use one or the other, not both.使用其中之一,而不是两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM