嗨,我正在嘗試按受歡迎程度獲取一些帖子,我需要它們是本月的帖子,并按保存的數量排序。
func fetch() {
let query = Firestore.firestore().collection("posts").whereField("timestamp", isGreaterThanOrEqualTo: Date().ThisMonth).order(by: "Likes", descending: true)
query.getDocuments { snapshot, error in
if error != nil {
print(error?.localizedDescription as Any)
return
}
guard let documents = snapshot?.documents else { return }
let data = documents.compactMap({ try? $0.data(as: Post.self) })
self.posts.append(contentsOf: data)
}
}
但是當我運行代碼時,我得到以下資訊:
*** 由于未捕獲的例外“FIRInvalidArgumentException”而終止應用程式,原因:“無效查詢。您在欄位“timestamp”上有一個不等式(notEqual、lessThan、lessThanOrEqual、greaterThan 或 greaterThanOrEqual)的 where 過濾器,因此您還必須使用“timestamp”作為您的第一個 queryOrderedBy 欄位,但您的第一個 queryOrderedBy 當前位于“喜歡”欄位上'反而。' 以 NSException 型別的未捕獲例外終止
我已經嘗試過這樣做,但它仍然不允許我這樣做:
func fetch() {
let query = Firestore.firestore().collection("posts").order(by: "timestamp", descending: true).whereField("timestamp", isGreaterThanOrEqualTo: Date().ThisMonth).order(by: "Likes", descending: true)
query.getDocuments { snapshot, error in
if error != nil {
print(error?.localizedDescription as Any)
return
}
guard let documents = snapshot?.documents else { return }
let data = documents.compactMap({ try? $0.data(as: Post.self) })
self.posts.append(contentsOf: data)
}
}
有沒有辦法以這種方式訂購帖子?我想要的是按人氣排序,但只有本月的帖子,而不是所有的帖子
謝謝!
uj5u.com熱心網友回復:
錯誤表示您的欄位名稱isGraterThenOrEqualTo:應該是greaterThanOrEqual:錯誤的,并且方法順序錯誤.order()。試試下面的例子。
let query = Firestore.firestore().collection("posts")
.order(by: "Likes", descending: true)
.whereField("timestamp", greaterThanOrEqual: Date().ThisMonth)
.order(by: "timestamp", descending: true) // first timestamp becouse of ".whereField()"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/412217.html
標籤:
下一篇:獲取并顯示用戶串列
