@IBAction func NextButton(_ sender: Any) {
//self.nextButton.isEnabled = false
if emailTF.text != "" && passwordTF.text != "" {
AuthProvider.Instance.registerButton(withEmail: emailTF.text!, password: passwordTF.text!, loginHandler: { (message) in
if message != nil {
self.alertTheUser(title:"Problem With Authentication", message: message!);
self.nextButton.isEnabled = true
} else {
let userID = FIRAuth.auth()?.currentUser?.uid
let userEmail = self.emailTF.text!
let userPassword = self.passwordTF.text!
//self.ref.child("UserEmails").child(userEmail).setValue(["UID": userID, "Email": userEmail,"Password": userPassword])
// app does not crash when I run the below reference to database.
self.ref.child("Users").child(userID!).setValue(["UID": userID,"Email":userEmail,"Password": userPassword])
// app crashes when I run the below reference to database.
self.ref.child("Email").child(userEmail).setValue(["UID": userID,"Email": userEmail,"Password": userPassword])
//self.nextButton.isEnabled = true
self.performSegue(withIdentifier: self.to_Select_Account_Segue, sender: nil);
}
我运行时,应用程序工作罚款
// app does not crash when I run the below reference to database.
self.ref.child("Users").child(userID!).setValue(["UID": userID,"Email":userEmail,"Password": userPassword])
虽然当我运行此代码时它会崩溃吗?
// app crashes when I run the below reference to database.
self.ref.child("Email").child(userEmail).setValue(["UID": userID,"Email": userEmail,"Password": userPassword])
答案 0 :(得分:1)
您无法使用Firebase database
符号在@
中创建子节点。请改用.childByAutoId
或uid
。
您应该像这样创建用户节点:
static var refToUsersNode = FIRDatabase.database().reference(withPath: "MainDataBase/users")
// MARK: - Create user after registration
static func create(with login: String) {
let loggedInUser = self.getCurrentUser()
let currentDate = Date()
let newUser = UserItem(uid: loggedInUser.uid, email: loggedInUser.email!,
login: login, createdDate: String(describing: currentDate))
// Create a child path with a key set to the uid underneath the "users" node
let refToNewUser = refToUsersNode.child(loggedInUser.uid)
refToNewUser.setValue(newUser.toAnyObject())
}
static func getCurrentUser() -> FIRUser {
return (FIRAuth.auth()?.currentUser)!
}
希望有所帮助