[英]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
.我还建议不要组合
await
和then
。 Use one or the other, not both.使用其中之一,而不是两者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.