如何使用Javascript从Firebase检索数据? [英] How to retrieve data from Firebase using Javascript?

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

问题描述

我正在尝试访问id并将其存储在变量中。到目前为止,我的代码是:

I'm trying to access "id" and store it in a variable. So far the code I have is:

var ref = firebase.database().ref("users");

ref.on("value", function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var childData = childSnapshot.val();
    console.log(childData);
  });
});

我从上面得到的是:

由于我可以访问对象本身,但如何访问属性id?我正在阅读官方文档,但我无法理解out。

With that I have access to the object itself, but how can I access the property "id"? I'm reading the official documentation but I can't figure it out.

推荐答案

这样做:

 var ref = firebase.database().ref("users");

ref.on("value", function(snapshot) {
 snapshot.forEach(function(childSnapshot) {
  var childData = childSnapshot.val();
  var id=childData.id;
  console.log(childData);
 });
});

在上面的位置是用户然后你使用 on()方法来读取数据,并使用 forEach 循环,在<$ c里面迭代$ c> pushid 并获取值。

In the above the location is at users then you use on() method to read data, and using the forEach loop, you iterate inside the pushid and get the values.

要单独访问每一个,请执行此操作 var ids = childData。 id; 或/和 var names = childData.name;

To access each one alone you do this var ids=childData.id; or/and var names=childData.name;

它也是最好使用 once()因为它只读取一次数据。

Also it is better to use once() as it only reads data once.

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

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