[英]Firebase Display Data from Database
<button id="all" onclick="button()"> View All </button>
<script>
function button(){
var userRef = new Firebase("https://addview-c21e6.firebaseio.com/users/");
userRef.on("value", function(snapshot) {
// The callback function will get called twice, once for "fred" and once for "barney"
snapshot.forEach(function(childSnapshot) {
// key will be "fred" the first time and "barney" the second time
var key = childSnapshot.key();
// childData will be the actual contents of the child
var childData = console.log(childSnapshot.val());
var para = document.createElement("p");
var node = document.createTextNode(childSnapshot.val());
para.appendChild(node);
var element = document.getElementById("viewAll");
element.appendChild(para);
});
});
}
</script>
<div id="viewAll"> </div>
The above code is what I have for my question. 上面的代码就是我要问的问题。 My tree is as follows: I have the root as users, then under that there is the username (ie john) and then under that there are two children.. Name and Age.
我的树如下所示:我具有root用户名,然后在该目录下有用户名(即john),然后在该目录下有两个孩子。名称和年龄。 So essentially I want the viewAll div to have all my data in the database be listed.
因此,基本上我希望viewAll div列出数据库中的所有数据。 So should list: John 15, Jake 16, and so on until it reaches the last user.
所以应该列出:John 15,Jake 16,依此类推,直到到达最后一个用户。 At the moment it is just printing object Object.
目前,它只是打印对象Object。 Where do I go from here?
我从这里去哪里? PS There is more code I'm just not showing it all for simplicity.
PS还有更多代码,为了简单起见,我不会全部显示。 I appreciate the help!
感谢您的帮助!
Your childSnapshot.val()
should be the JSON object with the username, name and age, assuming each of your user looks something like this: 您的
childSnapshot.val()
应该是具有用户名,名称和年龄的JSON对象,并假设您的每个用户都看起来像这样:
'fred': { username: 'fred', name: 'Fred Flintstone', age: 45 },
'barney': { username: 'barney', name: 'Barney Rubble', age: 44 }
In your forEach()
loop, you should be able to access each of the properties like this: 在
forEach()
循环中,您应该能够访问以下每个属性:
var userInfo = childSnapshot.val();
var username = userInfo.username;
var name = userInfo.name;
var age = userInfo.age;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.