I am trying to reference a variable that I am storing the users UID to in a document reference to Cloud Firestore. The path doesn't seem to read a string though. I have tried two different way and both ways give me errors. How would you pass a variable string into a Cloud Firestore document/collection path?
I am getting the UsernameID from the database with:
UsernameID = Auth.auth().currentUser!.uid
var UsernameID = String()
let ref = db.collection("user/\(UsernameID)/account").document("created")
ref.getDocument { (document, error) in
if let document = document, document.exists {
print("User Exists")
} else {
print("User does not exist in firestore")
}
}
Throws error:
*** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Invalid path (user//account). Paths must not contain // in them.'
libc++abi.dylib: terminating with uncaught exception of type NSException
var UsernameID = String()
let ref = db.collection("user").document(UsernameID).collection("account").document("created")
ref.getDocument { (document, error) in
if let document = document, document.exists {
print("User Exists")
} else {
print("User does not exist in firestore")
}
}
Throws 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 user has 1'
I have also tried storing the uid as a DocumentReferce but it failed to store a string as a DocumentReference.
let UsernameID = DocumentReference!