【问题标题】:Firebase orderByValue functionFirebase orderByValue 函数
【发布时间】:2021-09-17 14:15:29
【问题描述】:

我有一个数据库子结构,我在其中记录分配给每个管理员的报告数量

数据库子结构如下所示

ReportCount
       uuid string1: count1
       uuid string2: count2
       uuid string3: count3

我希望使用

检索此信息
firebase.database().ref('ReportCount').orderByValue().on("value", <Some Function>)

我希望这样做能够确定哪个管理员的报告数量最少,以便可以将新报告分配给该管理员。 但是,当我使用 orderByValue 时,我看到检索到的数据不是按计数值的升序或降序排序的。

这里有什么问题?

【问题讨论】:

  • Firebase 实时数据库和 Cloud Firestore 是两个独立的数据库。请仅使用相关标签标记您的问题,不要同时使用两者。
  • 请编辑您的问题以显示:1) 处理数据的代码,因为问题可能就在那里。 2) 实际的 JSON 作为文本,而不是您现在拥有的示意图显示。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。

标签: javascript firebase react-native firebase-realtime-database


【解决方案1】:

如果您需要让管理员手头的任务数量最少,那么您可以使用limitToFirst方法:

firebase.database().ref("/ReportCount").orderByValue().limitToFirst(1).once("value").then((snapshot) => {
  console.log(user.key, user.val())
})

如果您正在获取所有管理员并且数据未排序,则您的浏览器回答为here

如果您如图所示直接在快照上运行循环,则数据将按顺序记录:

firebase.database().ref("/ReportCount").orderByValue().once("value").then((snapshot) => {
  snapshot.forEach((user) => {
    console.log(user.key, user.val())
  })
})

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2017-09-10
    相关资源
    最近更新 更多