如何从Firebase数据库检索数据? [英] How to retrieve data from Firebase Database?

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

问题描述

我试图创建一个简单的程序,用户名,手机号码和用户的电子邮件地址,然后将数据放在Firebase实时数据库。



3个输入框和点击按钮进行上述操作。这里是代码:

 < input type =textid =name> 
< input type =textid =mobile>
< input type =textid =email>

< button type =buttonid =buttononclick =addLead()>提交< / button>

我已经设置了Firebase,如下所示:

 < script src =https://www.gstatic.com/firebasejs/3.2.0/firebase.js>< / script> 
< script>
//初始化Firebase
var config = {
...
};
firebase.initializeApp(config);

var database = firebase.database();
< / script>

这是我的 addLead()函数:

  function addLead(){
var clientName = document.getElementById('name')。value;
var clientMobile = document.getElementById('mobile')。value;
var clientEmail = document.getElementById('email')。value;

var newClientKey = database.ref()。child('leads')。push()。key;
database.ref('leads /'+ newClientKey +'/ name')。set(clientName);
database.ref('leads /'+ newClientKey +'/ mobile')。set(clientMobile);
database.ref('leads /'+ newClientKey +'/ email')。set(clientEmail);



$ b $ p
$ b这是我将数据放入数据库的方式。



没有任何帮助。这个生成的字符串可能是基于时间戳,如果是这种情况,那么再次生成它是不可能的。



当我检索如何获得电子邮件,手机和名称下这是唯一生成的字符串,这是只有在插入时才可用?

按上述结构,我需要得到2个级别访问所需的数据,因为缺乏文档,我不知道该怎么做,我不知道是否会这样的事情:

  database.child()子(); 


解决方案

这个问题的答案深埋在Firebase < a href =https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach =noreferrer>数据库参考。
forEach()用于在不知道孩子的确切路径的情况下到达孩子。

  var leadsRef = database.ref('leads'); 
leadsRef.on('value',function(snapshot){
snapshot.forEach(function(childSnapshot){
var childData = childSnapshot.val();
});
});

现在 childSnapshot 将包含所需的数据,同样的事情也可以通过使用 child_added

  leadsRef.on ('child_added',函数(快照){
//对数据
做某事});

唯一的区别是在 forEach(),它将从头开始循环,所以如果有新的数据被添加,它也会加载以前的数据,但是在 child_added 的情况下,监听器只是在新的孩子,而不是以前存在的孩子。


I'm trying to create a simple program that takes name, mobile number and email address from user and then puts the data on Firebase Realtime Database.

There are 3 input boxes and a button which on click does the above operation. Here is the code:

<input type="text" id="name">
<input type="text" id="mobile">
<input type="text" id="email">

<button type="button" id="button" onclick="addLead()">Submit</button>

I've set up Firebase like this:

<script src="https://www.gstatic.com/firebasejs/3.2.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    ...
  };
  firebase.initializeApp(config);

  var database = firebase.database();
</script>

And here is my addLead() function:

function addLead() {
    var clientName = document.getElementById('name').value;
    var clientMobile = document.getElementById('mobile').value;
    var clientEmail = document.getElementById('email').value;

    var newClientKey = database.ref().child('leads').push().key;
    database.ref('leads/'+newClientKey+'/name').set(clientName);
    database.ref('leads/'+newClientKey+'/mobile').set(clientMobile);
    database.ref('leads/'+newClientKey+'/email').set(clientEmail);
}

This is how I put data to database which works.

So the newClientKey is generating some string, it works well when inserting but now how to retrieve? The official documentation is of no help. This generated string might be based on timestamp, if that's the case then generating it again would be impossible.

When retrieving how will I get access to email, mobile and name under that uniquely generated string which was only available at the time of inserting?

Going by the above structure, I need to get 2 levels down to access the required data and because of the lack of documentation, I don't know how to do that, I'm not sure if something like this will work:

database.child().child();

解决方案

The answer to this question is buried deep within Firebase Database Reference. forEach() is used to get to the child without knowing the child's exact path.

var leadsRef = database.ref('leads');
leadsRef.on('value', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();
    });
});

Now childSnapshot will contain the required data, the same thing can also be accessed using child_added.

leadsRef.on('child_added', function(snapshot) {
      //Do something with the data
});

The only difference is that in case of forEach(), it will loop through from the start so if any new data will be added, it will also load the previous data but in case of child_added, the listener is only on new child and not the previous existing childs.

这篇关于如何从Firebase数据库检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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