cosmos cosmos - 1 month ago 20
Swift Question

Set Values when user is created on Firebase - Swift

I am trying to create a user in Firebase and save the user information at the time the user gets created. This is my action function where I create the user:

@IBAction func createAccountButton(_ sender: AnyObject) {

FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
if error != nil {
print(error!.localizedDescription)
} else {
let userID = (user?.uid)! as String
self.bringerUser.userRef = userID as String
let userEmail:String = self.emailTextField.text!
print(userEmail)
print(self.bringerUser.userRef! as String)
self.ref.child("users").child(self.bringerUser.userRef! as String).child("userInfo").setValue(["email": emailTextfield.text])
}
})
}


The user ID is nil when trying to access and write data on Firebase but if I print it it works fine. The error I get is this:


fatal error: unexpectedly found nil while unwrapping an Optional value

Answer

Try :-

FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).child("userInfo").setValue(["email": emailTextfield.text])
Comments