[英]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.