简体   繁体   English

询问所有父母及其子女Firebase

[英]Querying all parents and their children Firebase

I'm basically trying to list userIDs 1 at a time and also their children elements (firstname, last name, email , picture) so basically i want userid--> firstname,lastname,emailpicture looped for every user . 我基本上试图一次列出1个用户ID,以及他们的子元素(名字,姓氏,电子邮件,图片),所以基本上我想要userid - > firstname,lastname,emailpicture为每个用户循环。 this seems very difficult to do in firebase. 这在firebase中似乎很难做到。

layout of firebase ( i cannot change this ) : https://i.imgur.com/mJ5whBC.jpg firebase的布局(我不能改变这个): https//i.imgur.com/mJ5whBC.jpg

I'm not understanding how to grab all of the parents dynamically since they have randomly generated id's. 我不知道如何动态地抓住所有父母,因为他们随机生成了id。 i can do it one by one but i need to do it dynamically every time a user is added. 我可以一个接一个地做,但我需要在每次添加用户时动态地执行此操作。 Below is an example of what i want to do for each , but im manually inputting the ID so its pretty irrelevant. 下面是我想为每个人做的一个例子,但我手动输入ID,所以它非常无关紧要。

var database = firebase.database();

database.ref().once('value', function (data) {
picture = data.child("4320552453").child('picture').val();
firstName = data.child("4320552453").child('firstname').val();
lastName = data.child("4320552453").child('lastname').val();
email = data.child("4320552453").child('email').val();

$('#image').html('<img src="' + picture + '">')
$('#image').append(firstName)
$('#image').append(lastName)
$('#image').append(email)
my end goal is something like
div 1 userid 1 -> all information1 for each user
div 2 userid 2 -> all information  for each user
div 3 userid  3-> all information  for each user

If you want to loop over all child nodes in a DataSnapshot , use DataSnapshot.forEach . 如果要循环DataSnapshot所有子节点,请使用DataSnapshot.forEach So: 所以:

database.ref().once('value', function (snapshot) {
  snapshot.forEach(function(child) {
    picture = child.child('picture').val();
    firstName = child.child('firstname').val();
    lastName = child.child('lastname').val();
    email = child.child('email').val();
    console.log(snapshot.key, email, firstName, lastName, picture);
  });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM