我正在尝试查询用户ID数组,这些ID保留在Firestore数据库的一个数组中。它正在工作,并且已成功在我的控制台中显示UID。然后,我想查找字段“ uid”何时等于数组的成员。这是它停止工作的地方。我似乎无法真正将“ uid”与followedUID Array的成员进行匹配。
我在下面详细说明了我的代码。任何帮助将不胜感激:
func getFollowingPosts() {
db.collection("iAmFollowing").document(currentUserID!).getDocument { (document, error) in
if error != nil {
print("ERROR")
} else {
if let document = document {
let followedUID = document["uid"] as? Array ?? [""]
print("followed UID is \(followedUID)")
let searchedInfo = self.db.collection("posts").whereField("uid", isEqualTo: followedUID)
let refinedInfo = searchedInfo.order(by: "Alpha", descending: true)
refinedInfo.getDocuments { (documents, error) in
guard let documents = documents else {
print("NO DOCUMENTS")
return
}
for documents in documents.documents {
let title = documents.get("Title") as! String
let content = documents.get("Content") as! String
let username = documents.get("username") as! String
let postID = documents.get("postID")
let counter = documents.get("counter")
self.titleArray.append(title)
self.contentArray.append(content)
self.usernameArray.append(username)
self.postIDArray.append(postID as! Int)
self.effectsLabelArray.append(counter as! Int)
print(self.titleArray)
self.tableView.reloadData()
}
}
}
}
}
}
谢谢!
答案 0 :(得分:1)
您的查询要查询uid字段与给定值完全相等的文档:
db.collection("posts").whereField("uid", isEqualTo: followedUID)
如果uid字段是一个数组,则此查询将永远找不到任何东西,因为数组从不等于字符串。
如果要查看字段值是否为多个值之一,请使用"in" query:
db.collection("posts").whereField("uid", in: followedUID)