I am trying to add data by respective userIDs in Firebase by sign up the user, but it gives me error "unexpectedly found nil while unwrapping an optional value
" now I don't know what the matter is. But when I use code without adding userID
in ref
respectively the data is added successfully. but when I add userID
following ref
then got error.
SignUp
let userID = FIRAuth.auth()?.currentUser?.uid
ref.child("user_registration").child(userID!).setValue(["username": self.fullName.text, "email": self.emailTextField.text,"contact": self.numberText.text, "city": self.myCity.text, "state": self.countryText.text, "gender": genderGroup, "blood": bloodGroup])
Your user is not logged in
=>
error in unwrapping. You need to have something like:}
in your
AppDelegate
. It will change your initial view controller to login page, If user is not logged in.With this code you can force unwrapping.
Hope it helps
You need to understand the error. You are force unwrapping the
userID
which is not a good idea because the user may or may not be logged in when you calling this API. Below changes will resolve your issue.