ios - 如何使用 whereField 删除 Firestore 字段?
问题描述
当字段“uid”与当前用户的 ID 匹配时,我正在尝试删除文档中的字段。我非常坚持这一点,并希望得到任何帮助。我在下面详细介绍了我的代码以及我的数据库是如何设置的。
@IBAction func deleteAccountButtonIsTapped(_ sender: Any) {
let db = Firestore.firestore()
let userID = Auth.auth().currentUser?.uid
let username = usernameTextField.placeholder
Auth.auth().currentUser?.delete(completion: { (error) in
if error != nil {
print("ERROR MAIN SETTINGS 136")
} else {
db.collection("FollowerList").whereField("uid", isEqualTo: userID!).getDocuments { (snapshot, error) in
for snapshot in snapshot?.documents {
}
}
}
}
)}
我的数据库有一个集合“FollowerList”,其中的文档以用户的 UID 命名。在这些文档中有一个“uid”字段,其中包含用户 UID 的值。任何帮助将不胜感激。
解决方案
这应该做的工作:
func deleteAccountButtonIsTapped(_ sender: Any) {
let db = Firestore.firestore()
let userID = Auth.auth().currentUser?.uid
let username = usernameTextField.placeholder
Auth.auth().currentUser?.delete(completion: { (error) in
if error != nil {
print("ERROR MAIN SETTINGS 136")
} else {
db.collection("FollowerList").whereField("uid", isEqualTo: userID!).getDocuments { (snapshot, error) in
if let snapshot = snapshot?.documents {
for doc in snapshot {
//Do delete
db.collection("FollowerList").document(doc.documentID).updateData([
"fieldToDelete": FieldValue.delete(),
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
}
}
}
}
)}
有人会认为它可以像这样工作:但它不是“QueryDocumentSnapshot”类型的值,没有成员“updateData”。
func deleteAccountButtonIsTapped(_ sender: Any) {
let db = Firestore.firestore()
let userID = Auth.auth().currentUser?.uid
let username = usernameTextField.placeholder
Auth.auth().currentUser?.delete(completion: { (error) in
if error != nil {
print("ERROR MAIN SETTINGS 136")
} else {
db.collection("FollowerList").whereField("uid", isEqualTo: userID!).getDocuments { (snapshot, error) in
if let snapshot = snapshot?.documents {
for doc in snapshot {
// How one would think it works but it doesnt
doc.updateData([
"capital": FieldValue.delete(),
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
}
}
}
}
)}
有关详细信息,请参阅此页面: https ://firebase.google.com/docs/firestore/manage-data/delete-data#swift
推荐阅读
- javascript - 如何将常规函数高效地转换为异步生成器函数?
- r - 检查 paws.storage 是否可以连接到 S3
- java - 无法创建通知通道
- sql - Oracle中to_number的使用建议
- c++ - Cmake:带有 find_package 的 target_compile_definitions
- php - 如何在 laravel 7.0 中根据用户角色动态设置会话生存期
- winforms - 如何在 Windows 窗体运行时访问窗体资源?
- eclipse - 从 RCP 应用程序中启动 maven 构建
- javascript - Angular用html替换组件选择器
- firefox - Firefox 57+ WebExtensions 可以操纵 userChrome.css 吗?