我想检查我的 Firestore 查询是否找到了具有我想要的特定字段的任何文档。如果没有,我想继续编写其他代码。
不幸的是,我自己还没有找到解决这个问题的方法。你能帮忙吗?
代码:
Firestore.firestore().collection("conversations").whereField("mainUserID", isEqualTo: MainUID)
.whereField("otherUserID", isEqualTo: otherUserId).getDocuments { (snapshot, err) in
if snapshot.exists == true { // Value of type 'QuerySnapshot?' has no member 'exists'
} else {
}
}
最佳答案
来自文档
A FIRQueryDocumentSnapshot contains data read from a document in your Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted with the data property or by using subscript syntax to access a specific field.
A FIRQueryDocumentSnapshot offers the same API surface as a FIRDocumentSnapshot. As deleted documents are not returned from queries, its exists property will always be true and data: will never return nil.
重要的是这个
The document is guaranteed to exist
因此,由于保证快照的存在,.exists 选项没有意义。
一种方法是检查快照中有多少文档
if docs.count > 0 {
//there are docs
} else {
//there are no docs
}
关于ios - 检查 Firestore 查询 (whereField isEqualTo) 是否未找到任何文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63126931/