I’m switching data from Firebase Realtime Database to Firestore because I need more querying capabilities. However, I’m having trouble with saving my customer’s stripeID
to their collection document. My Cloud Function is hitting perfectly because Stripe is creating the customer correctly, but it’s not assigning to the collection reference. What do I need to fix so the collection reference could recognize the stripeID
as well? Thank you!
What I’m Seeing
Customer Model
struct Customer {
let uid: String
let stripeId: String
let fullname: String
let email: String
let username: String
let profileImageUrl: String
init(dictionary: [String : Any]) {
self.uid = dictionary["uid"] as? String ?? ""
self.stripeId = dictionary["stripeId"] as? String ?? ""
self.fullname = dictionary["fullname"] as? String ?? ""
self.email = dictionary["email"] as? String ?? ""
self.username = dictionary["username"] as? String ?? ""
self.profileImageUrl = dictionary["profileImageUrl"] as? String ?? ""
}
}
AuthServices
struct CustomerCredentials {
let email: String
let password: String
let fullname: String
let username: String
let profileImage: UIImage
}
static func createCustomer(credentials: CustomerCredentials, completion: CollectionCompletion) {
Auth.auth().createUser(withEmail: credentials.email, password: credentials.password) { result, error in
if let error = error {
debugPrint(error.localizedDescription)
return
}
guard let uid = result?.user.uid else { return }
let values = ["uid" : uid,
"email" : credentials.email,
"fullname" : credentials.fullname,
"username" : credentials.username,
"profileImageUrl" : profileImageUrl]
REF_CUSTOMERS.document(uid).setData(values, completion: completion)
}
}
}
RegistrationController
@objc func handleCreateAccount() {
let credentials = CustomerCredentials(email: email, password: password, fullname: fullname,
username: username, profileImage: profileImage)
AuthService.createCustomer(credentials: credentials) { error in
if let error = error {
Auth.auth().handleFireAuthError(error: error, vc: self)
self.showLoader(false)
return
}
Functions.functions().httpsCallable("createStripeCustomer").call(["email" : email]) { result, error in
if let error = error {
debugPrint(error.localizedDescription)
return
}
}
}
}
2
Answers
In my RegistrationController, I had to add an
updateData
value inside my Cloud Function code snippet in order to add it to my customer reference.RegistrationController
Assuming that
REF_CUSTOMERS
points to the "customers" collection, then when you’re using the following line of code:For writing the following values:
The document that will be added will always contain only the data that exists in the
values
object. Since there is nostripeId
present, the document that will be written will not contain thestripeId
. If you need to have thestripeId
as a field in the user document, then you have to add it like this: