2015-08-17 221 views
0

我在Firebase服務器中有一組歌曲。 每首歌都有lastCommentAt屬性,它是時間戳。Firebase按日期排序(時間戳) - Firebase

我想檢索到當前時間最近的lastCommentAt時間的歌曲順序。

這是我的火力地堡規則:

{ 
    "rules": { 
     ".read": true, 
     ".write": "auth != null", 
     "songs": { 
      ".indexOn": ["lastCommentAt"] 
     } 
    } 
} 

Songs feed example - CodePen

我不能使用orderByChild()。這使得奇怪的問題:

  • Uncorrect爲了

  • loadMore()加載相同的2首歌曲的循環。這意味着無限制的 歌曲列表。

Songs feed with orderByChild() example - CodePen

回答

0

我懷疑你的問題的根本原因是缺乏的startAt()的效果的認識連同orderByChild()使用時。欲瞭解更多信息,請閱讀Firebase documentation on queries

如果使用orderByChild("lastCommentAt")那麼startAt()功能適用到實例的ID,但每個實例的lastCommentAt財產。

所以一旦你已經添加orderByChild("lastCommentAt")那麼startAt()需要有第一個參數是你想獲得和第二個參數是你想獲得最後lastCommentAt值的第一個lastCommentAt值。