如何使用Javascript中的userID在Firebase中查询数据? [英] How to query data in Firebase using userID in Javascript?

查看:140
本文介绍了如何使用Javascript中的userID在Firebase中查询数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从用户节点"中查询用户信息,然后将其显示在html表中,到目前为止,我只能将Firebase数据库中请求节点"的状态子级查询到html表中.

I need to query the information of user from User Node and then display it in my html table, as of this moment I can only query the status child of the Request Node in my Firebase database to my html table.

如何查询其uid存在的每个节点中每个用户的信息? 这是我的结构,到目前为止,我可以显示待处理状态.由于用户只有一个uid,因此我认为可以在每个用户的待处理状态旁边显示他们的名字.

How do I query the information of every user in every node where his uid is present? Here is my structure, as of now I can display the pending status. Since the user has only 1 uid, I am thinking that diplaying the names of each user beside their pending status is possible.

] 这是我查询待处理状态的方法,

here's how I query pending,

var database = firebase.database().ref().child('Request').child('Pending');
database.once('value', function(snapshot){
    snapshot.forEach(function(data){
       var ReqStatus = data.val().request_status;
      content +='<tr>'
      content +='<td>' + ReqStatus+ '</td>'; 
      content +='</tr>';
});
    $('#mytable').append(content);
}
);

推荐答案

要同时为每个请求加载用户数据,您将需要使用其他侦听器.最简单的方法是这样的:

To also load the user data for each request, you'll need to use an additional listener. The simplest approach is something like this:

var root = firebase.database().ref();
var database = root.child('Request').child('Pending');
database.once('value', function(snapshot){
  snapshot.forEach(function(data){
      var reqStatus = data.val().request_status;
      var uid = data.key;
      root.child('User').child(uid).once('value', function(userSnapshot) {
        var username = userSnapshot.val().username;
        content += '<tr>'
        content += '<td>' + username+ '</td><td>' + reqStatus+ '</td>'; 
        content += '</tr>';
      })
  });
  $('#mytable').append(content);
});

有关此方法的一些注意事项:

A few notes on this approach:

  1. 您要对Firebase进行更多调用,但是由于
  1. You're performing a lot more calls to Firebase, but the performance will usually be quite good since Firebase pipelines those requests.
  2. If a user has multiple requests, you're loading the node for that user multiple times. If this is a real use-case for your app, you might want to keep a cache of recently loaded users.
  3. This code loads the entire user node. If you need just their name, you could listen on the username child node to reduce the bandwidth usage: root.child('User').child(uid).child('username').once('value'....
  4. As André commented: you might instead want to store the username of the author in each request node, to reduce the number of reads and keep the code simpler. To do that in JavaScript is fairly simple:

var root = firebase.database().ref();
var database = root.child('Request').child('Pending');
database.once('value', function(snapshot){
  snapshot.forEach(function(data){
      root.child('User').child(data.key).child('username').once('value', function(userSnapshot) {
        data.ref.update({ username: userSnapshot.val() });
      })
  });
});

这篇关于如何使用Javascript中的userID在Firebase中查询数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆