【问题标题】:Swift Firestore trouble deleting an element in an array nested in documentSwift Firestore 删除嵌套在文档中的数组中的元素时出现问题
【发布时间】:2021-05-27 01:53:14
【问题描述】:

感谢您查看不确定我缺少什么?

这是我的数据在 Firestore 中的样子

这里是评论模型。请注意,我应该在原始问题中显示这一点以获得更多上下文。

import SwiftUI
import FirebaseFirestoreSwift

struct Comment: Identifiable, Codable, Hashable {
    var id: String
    var userId: String
    var storyId: String
    var commentText: String
    var createdAt: Date
}

这是我删除 Firestore 数据的方法。没有错误被抛出?方法中的打印语句打印预期的 Firestore 文档 ID 和有问题的整个评论对象,但它也打印成功案例,并且 UI 或 FireStore 中没有任何变化。

func deleteComment(id: String, comment: Comment) {
        print(id, comment)
        //prints firestore document id and whole comment object
        
        let commentData: [String: Any] = [
            "id" : comment.id as Any,
            "userId" : comment.userId,
            "storyId" : comment.storyId,
            "commentText" : comment.commentText,
            "createdAt" : comment.createdAt
        ]
        
        store.collection(path).document(id).updateData([
            "comments" : FieldValue.arrayRemove([commentData])
        ]) { error in
            if let error = error {
                print("Unable to delete comment: \(error.localizedDescription)")
            }  else {
                print("Successfully deleted comment")
            }
        }
    }

【问题讨论】:

  • 您在第一个示例中使用“comment”(没有s),在第二个示例中使用“cmets`(带有s)。
  • @jnpdx 谢谢我修正了我的错字。不过我还有事情发生
  • 看起来arrayRemove 需要将整个对象传递给它——你只传递了id。
  • 这看起来可能是同一个问题:stackoverflow.com/questions/67733560/…
  • @jnpdx 谢谢我刚刚看到你的评论,没错,是日期类型

标签: ios swift firebase google-cloud-firestore


【解决方案1】:

事实证明,我的模型中 createdAt 属性的类型是 date。我编辑了我的原始问题以显示更多上下文的模型

所以我暂时只是删除了 createdAt 属性 感谢@jnpdx 提供的所有帮助。

 func deleteComment(docId: String,comment: Comment) {
        print("ID here",docId)
        print("Comment",comment)

        let commentData: [String: Any] = [
            "id" : comment.id,
            "userId" : comment.userId,
            "storyId" : comment.storyId,
            "commentText" : comment.commentText
        ]
   
        DispatchQueue.main.async {
            self.store.collection(self.path).document(docId).updateData([
                "comments" : FieldValue.arrayRemove([commentData])
            ]) { error in
                if let error = error {
                    print("Unable to delete comment: \(error.localizedDescription)")
                }  else {
                    print("Successfully deleted comment")
                }
            }
        }
       
    }

【讨论】:

    【解决方案2】:

    您只是从comments 部分中删除id。您应该像添加评论一样删除整个数据。

    
    func deleteComment(id: String, comment: Comment) {
            print(id, comment.id)
             // print results QDobPXyOM2WSLUA0j7Cj 
             //55BAE423-B32F-41E3-8B2CB594BF9002D7
    
            let commentData: [String: Any] = [
                "id" : comment.id as Any,
                "userId" : comment.userId,
                "storyId" : comment.storyId,
                "commentText" : comment.commentText,
                "createdAt" : comment.createdAt
            ]
    
            let docRef = store.collection(path).document(id)
    
            docRef.updateData([
                "comments" : FieldValue.arrayRemove([commentData])
            ])
        }
    

    【讨论】:

    • 我尝试添加整个对象仍然没有任何反应
    • 我重构了我的问题也添加了一个补全
    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2021-02-26
    相关资源
    最近更新 更多