Lazy load Firebase

允我心安 提交于 2019-12-13 07:26:17

问题


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

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, messages: FirebaseListObservable

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 is what I tried:

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

'start' here is an id like '-KovtCl4hEUPR2LfQfpc', which is the id of the oldest message in the this.messages FirebaseListObservable. I've also tried to manually pass this ID as string to the method that executes the query above.

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

The last query gives me 0 results.

Firebase layout

/messages/conversationId/messageId/{message}

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

What am I doing wrong here?


回答1:


Consider using "endat" as well.

Have a look at this tutorial for detailed instructions.

PS I'm on the phone, I'll reformat in a bit.




回答2:


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

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.



来源:https://stackoverflow.com/questions/45081212/lazy-load-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!