2

I'm trying to build a shopping list with Firestore. A user belongs to a group. A group has a shopping list. A shopping list has items. A path to a shopping list may look like this /groups/A11C1F3A/ShoppingListA11C1F3A/ where A11C1F3A is the groupId.

I'm getting an error

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but groups has 1'

and as far as I understand this error it means that there must be something wrong with my data model. I found this question where the accepted answer is saying

A valid path to a collection will always have an odd number of segments; a valid path to a document, an even number.

This is my method:

  func saveItemToDB(with itemName: String) {
        let db = Firestore.firestore()
        if let _ = Auth.auth().currentUser {
            let groupId = HomeViewController.groupId
            let groupRef = db.collection("groups").document(groupId).collection("ShoppingList" + groupId)    
            let data: [String: Any] = [
                "name" : itemName
            ]
            groupRef.document(itemName).setData(data)
        }
    }

The number of segments to my collection is three right? I don't see the problem here..

Marcel Braasch
  • 1,083
  • 1
  • 10
  • 19
  • 2
    What exactly is the value of `groupId`? Please be absolutely certain what it is at runtime. Use a debugger or log statement. Also please be sure this isn't due to some other reference in your code. – Doug Stevenson Aug 29 '19 at 23:47
  • 1
    God damn it. Sometimes I'm too stupid for this.. so I set this ViewController as initial viewController for debugging purpose. I only have one group so in some other method I hardcoded that groupId. For this VC I forgot to, and it's still that static variable. But since HomeViewController is never initialized obviously groupId was "".. sometimes its too obvious I guess. Thanks Doug! – Marcel Braasch Aug 29 '19 at 23:53

0 Answers0