Firestore订购查询无法像Firebase一样工作?

时间:2018-07-06 04:06:14

标签: swift firebase firebase-realtime-database google-cloud-firestore

我用来观察帖子的Firebase模型是这样的:

let postReference = Database.database().reference().child("posts")
    let watchedByQuery = postReference.queryOrdered(byChild: "/watchedBy/" + UID!).queryStarting(atValue: "")

但是,当我尝试将其转换为Firestore时,

let postsReference = Firestore.firestore().collection("posts")
        let watchingQuery = postsReference.order(by: "/watchedBy/" + UID!).start(at: [""])
    watchingQuery.addSnapshotListener { (querySnapshot, error) in
        guard let snapshot = querySnapshot else {
            print("Error fetching snapshots: \(error!)")
            return
        }
        snapshot.documentChanges.forEach { diff in

我收到错误Invalid field path (/watchedBy/P02bS6QrVE122yjrU7L87X2). Paths must not contain '~', '*', '/', '[', or ']''

似乎是什么问题?本质上,该查询检查在watchedBy部分中具有用户ID的帖子。但是,FireStore转换似乎无效。

1 个答案:

答案 0 :(得分:1)

本质上,我只是添加了一个whereField,以便存在watchedBy / UID。

let postsReference = Firestore.firestore().collection("posts") 
let watchingQuery = postsReference.whereField("watchedBy."+(UID!), isGreaterThan:"")

编辑:

请注意更改自

"/watchedBy/" + UID!

设置为格式正确的Firestore子引用

"watchedBy."+(UID!)