0

I am trying to add to an array of maps in Firestore and getting the following error:

Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: __SwiftValue'

This is my Model Class:

struct StringModel: Identifiable, Codable, Equatable, Hashable {   
    var id = UUID()
    var cross: Int
    var mains: Int
    var name: String
    var date: Date
}
struct UserDataModel: Identifiable, Codable {
    @DocumentID public var id: String?
    var uid: String
    var strings: [StringModel]?
}

Am using the following function to update the value in firestore, however, it doesnt seem to work and throws the error:

@Published var newString = StringModel(cross: 50, date: Timestamp(), mains: 50, name: "")

func addString() {
    db.document(uidStr).updateData(["strings" : FieldValue.arrayUnion([newString])]) { err in
        if let err = err{
            print("\(err)")
        }
    }
}

Firestore Datatbase

Any ideas how I can go about this? Thanks!

5
  • did you try to print the actual error? print("Failed due to: \(error)") Commented Apr 9, 2022 at 18:59
  • Yes I did. Same error. Additionally I have no problem reading data from Firestore its just updating array data. Commented Apr 9, 2022 at 19:18
  • Please edit your question and add your database structure as a screenshot. Commented Apr 10, 2022 at 8:30
  • We don't know what this FieldValue.arrayUnion([newString])]) is... [newString]? can you update your question explaining what that is? Commented Apr 10, 2022 at 13:26
  • Your code isn't matching up with the structure in your question. The data in strings is being written but there's an id field which is not shown in the structure and the UserDataModel doesn't appear to have a yob field. We still don't know what this FieldValue.arrayUnion([newString])] is so the question is very confusing. Can you clarify what's being asked? Commented Apr 11, 2022 at 17:24

1 Answer 1

3

You can't append a Swift object to a Firestore array. You must convert the Swift object into a [String: Any] object. You can add a computed property to the model that outputs this for you.

struct StringModel: Identifiable, Codable, Equatable, Hashable {
    var id = UUID()
    var cross: Int
    var mains: Int
    var name: String
    var date: Date
    
    var firestoreData: [String: Any] {
        return [
            "id": id,
            "cross": cross,
            "mains": mains,
            "name": name,
            "date": date
        ]
    }
}

func addString() {
    db.document(uidStr).updateData(["strings" : FieldValue.arrayUnion([newString.firestoreData])]) { err in
        if let err = err{
            print("\(err)")
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.