【发布时间】:2017-10-25 17:39:50
【问题描述】:
当我在userID 下保存数据时,数据没有被存储到 Firebase 中并给出错误unexpectedly found nil while unwrapping an optional value,但是当我使用childByAutoID 时,数据被成功存储。帮我保存在userID 节点下。在这里,我解释说,当我在注册操作下创建用户时,会发生这种情况。
@IBAction func createAccountAction(_ sender: Any) {
if self.emailTextField.text == "" || self.passwordTextField.text == "" {
let alertController = UIAlertController(title: "Error", message: "Please enter your email and password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
} else if (self.passwordTextField.text != self.retypePasswordfield.text) {
let alertController = UIAlertController(title: "Error", message: "Password does not match", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
} else {
ref.child("user_registration").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])
FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if error == nil {
FIRAuth.auth()?.currentUser!.sendEmailVerification(completion: { (error) in
})
print("You have successfully signed up")
let alertController = UIAlertController(title: "Successful!", message: "Email Verification link sent", preferredStyle: .alert)
let alertActionOkay = UIAlertAction(title: "Okay", style: .default) { (action) in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoginFirstViewController")
self.present(vc!, animated: true, completion: nil)
}
alertController.addAction(alertActionOkay)
self.present(alertController, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
【问题讨论】:
-
您的用户未通过身份验证,因此请先对其进行身份验证。这可能是个问题。因为它没有获取当前用户。
-
为了帮助您进行调试,您可以将这样的行拆分为多个部分,以便查看错误发生的位置。将uid提取到变量中,定义引用,提取文本字段数据,然后将它们放在一起。也就是说,正如@dahiya_boy 所建议的那样,您应该始终从您正在解包的任何内容开始,并且看起来当前用户确实没有经过身份验证
-
@dahiya_boy 用户正在注册,因此用户未通过身份验证
-
@Russell 用户正在注册,所以用户没有经过身份验证,我做了和你说的一样的事情,但仍然是同样的错误,因为用户是注册的。有什么建议吗?
-
您应该在 Auth 委托方法中调用此方法。其中一个会告诉您用户何时成功注册并将该用户返回给您
标签: ios firebase swift3 firebase-realtime-database firebase-authentication