0

Thanks for having a look not sure what I am missing?

Here is what my data looks like in Firestore

enter image description here

Here is Comment Model. Note I should have shown this in original question for more context.

import SwiftUI
import FirebaseFirestoreSwift

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

Here is method where I am deleting Firestore data. No error is being thrown? The print statements in method print the expected firestore document id and the whole comment object in question But it also prints the success case and nothing changes in UI or 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")
            }
        }
    }
5
  • You're using "comment" (no s) in your first example and "comments` (with an s) in your second one. Commented May 27, 2021 at 4:30
  • @jnpdx Thanks I fixed my typo. I still have something going on though Commented May 27, 2021 at 5:39
  • It looks like arrayRemove needs the whole object passed to it -- you're only passing the id. Commented May 27, 2021 at 6:26
  • This looks like it may be the same issue: stackoverflow.com/questions/67733560/… Commented May 28, 2021 at 17:51
  • @jnpdx Thanks I just saw your comment, that is correct it was the date type Commented May 29, 2021 at 19:09

2 Answers 2

1

It turns out it was a type thing the createdAt property in my model is of type date. I edited my original question to show model for more context

So I for time being I just removed the createdAt property Thanks to @jnpdx for all the help.

 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")
                }
            }
        }
       
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You are only removing the id from the comments section. You should remove the whole data as you do when you add a comment.


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])
        ])
    }

2 Comments

I tried adding whole object still nothing happens
I refactored my question added a completion also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.