0
votes

My intended flow is that someone reaches the signup page, enters their email and password, then when it is successfully processed then create a user in Firestone. After a lot of print lines, I can see the authentication part works just fine, but when I try to create the user in firestore it crashes. I suspect it's an async issue because I try to write the below code, but currUser?.uid is nil. Please help!

db.collection("users").document(currUser?.uid ?? "").setData(user) { err in

This is also how I'm trying to combat the async issue. The below is the code for the sign up button

            Button(action: {
                self.signUp()
                self.viewModel.createUser()
            })

I've also attached the error I am getting below:

2020-06-21 23:28:20.163164-0400 Routines[5662:144417] *** Assertion failure in firebase::firestore::api::DocumentReference::DocumentReference(model::ResourcePath, std::shared_ptr)(), /Users/paulachan/Documents/Apps/Routines/Pods/FirebaseFirestore/Firestore/core/src/api/document_reference.cc:67 2020-06-21 23:28:20.241127-0400 Routines[5662:144417] *** 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 users has 1'

1

1 Answers

1
votes

The error message means that currUser is null, so db.collection("users").document(currUser?.uid ?? "") evaluates to db.collection("users").document("") which is invalid.

Based on the code you shared we can't say why currUser is null, but it typically means that you're trying to access Auth.auth().currentUser before it is initialized. I'd recommend using the first code snippet in the documentation on getting the current user as it ensures your code only executes once the user sign in is completed:

Auth.auth().addStateDidChangeListener { (auth, user) in
  if (user != nil) {
    db.collection("users").document(user?.uid ?? "").setData(user) { err in
      ...
    }
  }
}