"Cannot convert value of type 'String' to expected argument type '[Any]'" error when trying to add array element in firestore in swift app

Viewed 22

when trying to add an element to an existing array in firestore I get the error "Cannot convert value of type 'String' to expected argument type '[Any]'"

this is the method where the error occurs:

func addEventIdToUser(eventID: String) async {
        let db = Firestore.firestore()
        let userReference = try await db.collection("user").document(userID)
        try? await userReference.updateData(["events": FieldValue.arrayUnion(eventID)])
  
    }

does anyone know how to solve the issue?

1 Answers

SOLUTION:

I forgot brackets around eventID

this is the right way:

  try? await userReference.updateData(["events": FieldValue.arrayUnion([eventID])])
Related