How to get id in reactjs

Viewed 42
  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({});
      }
    })

  }

enter image description here

1 Answers

If you want to log the value of the snapshot that you get back from the database, use its val() method:

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. Use one or the other, not both.

Related