【问题标题】:How to check child if it is exist in firebase list using angularfire2如何使用angularfire2检查孩子是否存在于firebase列表中
【发布时间】:2018-07-20 23:09:21
【问题描述】:

我需要使用 angularfire2 检查 firebase 列表是否存在。

这是我的父母名单:

我需要检查子 Messages 是否存在。

这是我的代码:

  checkChild(){
    let usersRef = this.db.list(`/Users/${this.authData.afAuth.auth.currentUser.uid}/`);
  }

我尝试输入一些方法,例如userRef.child('Messages')userReflistChild('Messages'),但我收到一个错误,指出最后一个方法不存在。

我需要避免使用 subscribe 方法,我可以使用 map 方法解决,但我正在寻找新方法和 firebase 最佳实践。

【问题讨论】:

  • 我试图避免使用订阅方法。对于程序的复杂性。
  • 如果您避免订阅,那么您使用什么方法来检索数据(例如一次)?
  • Yes once 例如。我正在寻找像 child hasChildren() 这样的方法。我不认为 angularfire2 支持这种方法。所以也许我会使用 FireBase 库。
  • 如果您致电 (/Users/${this.authData.afAuth.auth.currentUser.uid}/Messages) 但它不存在会怎样?如果它返回 null 或 undefined 您可以使用它来验证节点的存在
  • 我相信这取决于您的用例,并且对于这种特定情况没有最佳实践,但当然这与您要完成的工作有关。

标签: angular typescript firebase firebase-realtime-database angularfire2


【解决方案1】:

我没有看过 AngularFire2,但是通过 FireBase 实时数据库文档,您可以使用此处描述的“hasChild()”方法。

Firebase Documentation, Datasnapshot, hasChild

【讨论】:

  • 我没有在 angularfire2 文档中找到这个 hasChild,所以我使用了 FireBased 数据库文档。谢谢
  • 如果你觉得我的问题很清楚,请投赞成票。
猜你喜欢
  • 2017-04-27
  • 2017-02-25
  • 2017-03-14
  • 1970-01-01
  • 2016-09-18
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多