Firebase - 基于分区偏移逻辑的查询 [英] Firebase - Query based on partitioned offset logic

查看:26
本文介绍了Firebase - 基于分区偏移逻辑的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Angular 4 中创建一个聊天应用,用于在 Firebase 数据库中存储和读取消息.

I'm creating a chat app in Angular 4 which stores and reads messages in and from the Firebase database.

我编写了一个查询来仅检索该特定对话的最新 10 条消息.

I've written a query to retrieve only the latest 10 messages of that specific conversation.

this.messages = this.db.list('messages/' + conversationId, { query: { limitToLast: 10 } });

db:AngularFireDatabase,消息:FirebaseListObservable

现在我想在有人按下更多"按钮(或滚动到顶部)时检索 10 条较旧的消息,但我在为此编写查询时遇到了困难.

Now I want to retrieve the 10 older messages when somene presses the 'more' button (or scrolls to the top) and I'm having difficulties writing a query for this.

这是我试过的:

this.messages = this.db.list('messages/' + conversationId, { query: { orderbyChild: 'id', startAt: start, limitToLast: 10 } });

这里的'start'是一个像'-KovtCl4hEUPR2LfQfpc'这样的id,它是this.messages FirebaseListObservable中最早消息的id.我还尝试将此 ID 作为字符串手动传递给执行上述查询的方法.

尝试按照此处所述进行操作:

Trying to do it as described here:

  1. https://stackoverflow.com/a/43969460/5437768
  2. https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query
  3. https://firebase.google.com/docs/database/web/lists-of-data#filtering_data

最后一个查询给了我 0 个结果.

The last query gives me 0 results.

Firebase 布局

/messages/conversationId/messageId/{message}

/messages/conversationId/messageId/{message}

"messages": {
    "-k32b": {
        "-kT3d": {
            "content": "Hello World!",
            "id" : "-kT3d"
        },
        "-kT4c": {
            "content": "How are you World?",
            "id" : "-kT4c"
        }
    }
}

我在这里做错了什么?

推荐答案

由于查询中的拼写错误('orderbyChild' 而不是 'orderByChild'),结果证明它不起作用.感谢 Firebase 在这里没有给我任何错误:p

It turns out it did not work because of a typo in the query ('orderbyChild' instead of 'orderByChild'). Thanks Firebase for not giving me any errors here:p

我现在确实在一个方法链中同时使用 .orderByChild()、.endAt() 和 .limitToLast().这可确保以正确的顺序执行查询条件,而在查询对象中查询 this 似乎没有按特定顺序执行这些条件.

I am now indeed using .orderByChild(), .endAt() and .limitToLast() together, in a method chain. This makes sure that the query-conditions are executed in the correct order, where as querying this in a query object seems to execute these conditions in no particularly order.

这篇关于Firebase - 基于分区偏移逻辑的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆