简体   繁体   English

在 firebase (javascript) 中,如何获取当前登录用户的数据?

[英]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 ?) (我怎样才能获得当前用户的按键?)

  • I'm not an experienced programmer (less than a year).我不是一个有经验的程序员(不到一年)。 Please use a language that is easy to understand.请使用易于理解的语言。
  • I've gone through all the firebase documentation.我已经浏览了所有的 firebase 文档。 Please don't just give me the copy of the documentation or the link to it.请不要只给我文档的副本或指向它的链接。

Screenshot: Firebase realtime database屏幕截图:Firebase 实时数据库

Screenshot: JavaScript function for retrieving current user's data屏幕截图:用于检索当前用户数据的 JavaScript 函数

Additional JavaScript screenshot附加 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.用户数据是您选择存储它的任何地方——除了nameemail和其他一些字段之外, 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.

相关问题 Thingsboard 问题:如何在自定义小部件中获取当前登录的用户 - Thingsboard issue: How Can I get the currently logged in user in my custom widget 如何将当前登录的用户添加为 Firestore 中的字段 - How can I add the currently logged in User as a field in Firestore 如何从Javascript中的Firebase数据库中删除用户的信息(即使他/她当前未登录)? - How to delete a user's info(even if he/she is not currently logged in) from firebase database in javascript? 如何检索当前从 Firestore/Firebase 登录的用户信息? - How to retrieve the user's info that is currently logged in from Firestore/Firebase? 如何验证用户通过javascript中的Firebase登录 - How to verify user is logged in through firebase in javascript 如何检查用户当前是否正在登录并正在玩Facebook应用程序游戏 - How can I check if a user is currently logged in and playing a facebook app game 如果用户当前在页面上处于活动状态,我如何使用 JavaScript/jQuery 进行检测? - How can I detect with JavaScript/jQuery if the user is currently active on the page? 如何在 Javascript 的 Keycloak 中获取当前登录用户的密码? - How can I get the current logged in user's password in Keycloak in Javascript? 如何查看用户是匿名用户还是从javascript登录? - How can I check to see if the user is anonymous or logged in from javascript? 在页面加载之前获取登录用户 Javascript Firebase - Get logged in user before page load Javascript Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM