首页 > 解决方案 > 如何使用 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 的值。任何帮助将不胜感激。

标签: iosswiftfirebasegoogle-cloud-firestore

解决方案


这应该做的工作:

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


推荐阅读