【问题标题】:Size of child in firebaseFirebase中孩子的大小
【发布时间】:2018-07-14 01:53:01
【问题描述】:

我正在使用对 firebase 实时数据库的反应

所以我有这个 firebase 数据,我想计算关注者的总值。但是,我所知道的是,firebase 没有这样的计数功能,您需要手动执行。

从下面我可以看到所有用户数据,例如用户名和电子邮件。但是,我仍然无法弄清楚如何计算关注者的数量。

我希望有人可以提供帮助。谢谢。

const ref = firebaseDB.ref();
ref
  .child("Users")
  .orderByChild("email")
  .equalTo(this.state.currentUserEmail)
  .once("value", snapshot => {
    let userData = {};
    for (var childSnapshot in snapshot.val()) {
      userData.key = childSnapshot;
      // then loop through the keys inside the child object and get the values like price (key) : 44.95 (value)
      for (var value in snapshot.val()[childSnapshot]) {
        console.log(value);
        console.log(snapshot.val()[childSnapshot][value]);
        userData[value] = snapshot.val()[childSnapshot][value];
      }
    }
    console.log(userData);
  });

【问题讨论】:

    标签: javascript reactjs firebase-realtime-database


    【解决方案1】:

    这样的事情应该可以解决问题:

    const ref = firebaseDB.ref();
    ref
      .child("Users")
      .orderByChild("email")
      .equalTo(this.state.currentUserEmail)
      .once("value", snapshot => {
        snapshot.forEach((childSnapshot) => {
          console.log(childSnapshot.child("followers").numChildren());
        }
        console.log(userData);
      });
    

    更改/添加:

    1. 使用DataSnapshot.forEach() 以更简洁的方式循环孩子。
    2. 使用DataSnapshot.child() 获取followers 节点的快照。
    3. 使用DataSnapshot.numChildren() 确定关注者数量。

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 2018-04-27
      相关资源
      最近更新 更多