问题描述
我正在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 } });
开始"此处是一个类似于"-KovtCl4hEUPR2LfQfpc"的ID,它是this.messages FirebaseListObservable中最旧消息的ID.我还尝试过手动将此ID作为字符串传递给执行上述查询的方法.
尝试按照此处所述进行操作:
Trying to do it as described here:
- https://stackoverflow.com/a/43969460/5437768
- https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query
- 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().这样可以确保查询条件以正确的顺序执行,而在查询对象中查询似乎没有特别的顺序执行这些条件.
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-基于分区偏移量逻辑的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!