javascript - OrderByChild、equalTo 和 limitToFirst 查询加载整个数据库

标签 javascript firebase firebase-realtime-database

我正在尝试在 firebase 中制作一个区域词典应用程序。

数据结构图像:Data structure

用于分页的 OrderByKey 和 limitToFirst 查询工作正常,它仅加载查询的数据。下面的代码示例 -

db.ref('dictionary')
  .orderByKey()
  .startAt(String(this.perpage * (this.page - 1) + 1))
  .limitToFirst(this.perpage)
  .once('value')
  .then((snapshot) => {
    vm.words = []
    snapshot.forEach(function (childSnapshot) {
      var childKey = childSnapshot.key
      var childData = childSnapshot.val()
      childData.id = childKey
      vm.words.push(childData)
    })
    vm.inProgress = false
  })

但是,当我使用 orderByChild('word').equalTo(search_query).limitToFirst(3) 查询时,它会一次获取所有 176023 条记录(从 websocket 检查)。下面的示例代码 -

db.ref('dictionary')
  .orderByKey()
  .orderByChild('word')
  .equalTo(this.search + '')
  // .startAt(String(25 * (this.page - 1) + 1))
  .limitToFirst(3)
  .on('value', (snapshot) => {
    console.log(snapshot)
    snapshot.forEach(function (childSnapshot) {
      var childKey = childSnapshot.key
      var childData = childSnapshot.val()
      childData.id = childKey
      vm.words.push(childData)
    })
    vm.inProgress = false
  }, (error) => {
    console.log(error)
    vm.inProgress = false
  })

最佳答案

很可能您没有关于 word 的索引。当没有索引时,服务器将整个位置发送给客户端,然后客户端进行过滤。在这种情况下,您还会在 JavaScript 控制台中收到非常明确的警告。

请参阅section in the Firebase documentation on how to define indexes .

关于javascript - OrderByChild、equalTo 和 limitToFirst 查询加载整个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44301826/

相关文章:

javascript - 如果带有调试器语句 Javascript 的代码部署在生产环境中,会带来多少安全问题?

javascript - 如何在点击其他图像时更改div的图像

javascript - 如何从给定的数字获取数字格式和区域设置字符串?

android - 未能获取 FirebaseDatabase 实例。在 FirebaseApp 中指定 DatabaseURL

ios - 为什么在遍历 firebase 数据库时得到 nil?

firebase - 从 NodeMCU 发送到 Firebase 时如何构建我的传感器数据?

javascript - 如何使用 AJAX 发布数据来更新我的 MVC View

ios - 应用程序崩溃。日志显示原因 : 'Invalid query. You must not specify a starting point before specifying the order by.'

firebase - Flutter 应用程序作为 .apk 工作,但 "installation failed"使用 firebase 分发

javascript - 用于调查结果的 Firebase 数据库