[英]In firebase (javascript), how can I get data for the currently logged in user?
I've spent a few days researching for the solution.我花了几天时间研究解决方案。 I have found many answers that were quite helpful, but still I haven't been able to find a solution that fully resolved my problem.
我找到了许多非常有帮助的答案,但我仍然无法找到完全解决我的问题的解决方案。 Basically all I want to do is to get current user's data (stored in firebase realtime database) and display them on the user's screen.
基本上我想要做的就是获取当前用户的数据(存储在 firebase 实时数据库中)并将它们显示在用户的屏幕上。
So far, I can display data for all the user.到目前为止,我可以显示所有用户的数据。 But how can I have it display data for only the current user ?
但是我怎样才能让它只显示当前用户的数据? (How can I get the push key of only the current user ?)
(我怎样才能获得当前用户的按键?)
Screenshot: Firebase realtime database屏幕截图:Firebase 实时数据库
Screenshot: JavaScript function for retrieving current user's data屏幕截图:用于检索当前用户数据的 JavaScript 函数
User data is wherever you choose to store it--other than name
, email
, and a few other fields, the User object cannot actually store any other info.用户数据是您选择存储它的任何地方——除了
name
、 email
和其他一些字段之外, User 对象实际上不能存储任何其他信息。 Therefore, you will need to decide where in the database you want to keep your extra data.因此,您需要决定将额外数据保存在数据库中的哪个位置。
Assuming you choose to create a collection users
with children named by uid
, and you store the users' countries in those documents, the following will get you a specific user's data and you can use whatever fields you'd like from there:假设您选择创建一个带有以
uid
命名的孩子的集合users
,并且您将用户的国家/地区存储在这些文档中,以下内容将为您提供特定用户的数据,您可以从那里使用您想要的任何字段:
let ref = database.ref('/users/' + currentUser.uid).once('value').then(function(snapshot) {
let userData = snapshot.val();
console.log(userData.country);
}
Here you go!干得好!
function showUser(){
var usref=firebase.database().ref("countries/"+uid);
//uid here is the unique code u gave to the specif user
usref.once('value', function(snapshot) {snapshot.forEach(function(childSnapshot) {
if((childSnapshot.key)=="country"){
var country = childSnapshot.val();
//use this variable here enter code here
window.alert(country);//just for checking!!!
}
if((childSnapshot.key)=="age"){
var name = childSnapshot.val();
//use this variable here
window.alert(name);//just for checking!!!
}
});
});
}
I hope this code works for you.我希望这段代码对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.