简体   繁体   English

如何在我的网站上显示Firebase数据库中的数据?

[英]How can I display the data from my Firebase Database in my web site?

On Firebase, I am having the following issue; 在Firebase上,我遇到了以下问题; my data being structured as this: 我的数据结构如下:

myApp
- myList
    - KKJJXX...1        myString: "String One"
    - KKJJXX...2        myString: "String Two"
    - KKJJXX...3        myString: "String Three"

I want to make some JavaScript (in a web page of mine) to list the values of myString in the DB above. 我想制作一些JavaScript(在我的网页中)列出上面数据库中myString的值。 Here is what I came up with after digging the web. 这是我在挖掘网络后想出的。 It works to a point, but not quite. 它起作用,但不完全。 I can see in the debugging console of my browser that I have got the data I want. 我可以在浏览器的调试控制台中看到我有我想要的数据。 I write my concerns more precisely after the script. 在剧本之后我更准确地写下我的疑虑。

<script>
var ref = new Firebase("https://myApp.firebaseio.com/myList/");

ref.on("value", function(snapshot) {
    for (x in snapshot.val()) {
        var xRef = new Firebase("https://myApp.firebaseio.com/myList/"+x+"/");
        xRef.once("value", function(xsnapshot) {
            var name = xsnapshot.child("myString");
            console.log(name);
        });
    }
}, function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});
</script>

In the debugging console I get this line(3 times): 在调试控制台中,我得到这一行(3次):

Object { A: Object, Y: Object, g: Object }

And I can see that the A Object contains the information I am looking for (myString). 我可以看到A对象包含我正在寻找的信息(myString)。 What should I change in the script above to list directly the contents of myString on the console? 我应该在上面的脚本中更改什么来直接列出控制台上myString的内容?

When you call child , you're getting a new FirebaseReference -- not a data snapshot. 当您致电child ,您将获得新的FirebaseReference - 而不是数据快照。 You'd need to .on('value') it to fetch the ref's data, but that would be silly because you already have the data. 你需要.on('value')来获取ref的数据,但这很愚蠢,因为你已经掌握了数据。

Instead, just stash the snapshot's data in a local variable and operate over that. 相反,只需将快照的数据存储在本地变量中并对其进行操作即可。

ref.on("value", function(snapshot) {
    for (x in snapshot.val()) {
        var xRef = new Firebase("https://myApp.firebaseio.com/myList/"+x+"/");
        xRef.once("value", function(xsnapshot) {
            var data = xsnapshot.val();
            var name = data["myString"];
            console.log(name);
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的 firebase 数据库中检索数据? - How can I retrieve data from my firebase database? 如何显示 Firebase 数据库中的数据? - How can I display data from Firebase database? 如何将数据库中的数据导入 JavaScript? - How can I import data from my database into my JavaScript? 我无法将数据从Firebase显示到我的Node JS客户端 - I can't get my data to display from firebase to my node js client 如何将数据从 swift 发送到 javascript 并在我的 Web 视图中显示它们? - How can I send data from swift to javascript and display them in my web view? 在我的网站上显示 Google Analytics 数据? - Display Google Analytics data on my web site? 如何使用Google Analytics(分析)中的数据显示我网站上的页面访问? - How to display page visits in my web site using data from Google Analytics? 如何从 Firebase 获取数据到我的 React 应用程序中 - How can I fetch data from Firebase into my React App 我想从 Firebase 实时数据库中获取数据并希望在我的 HTML 页面中显示 - I want to get data from Firebase realtime database and wants to display in my HTML page 我无法使用 firebase 从我的文档中获取我的数据 - I can't get my data from my document with firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM