swift - 由于未捕获的异常而终止应用程序'(child :)必须是非空字符串且不包含'。' '#''$''['或']''

时间:2017-05-23 10:10:29

标签: swift database xcode firebase firebase-authentication

@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])

1 个答案:

答案 0 :(得分:1)

您无法使用Firebase database符号在@中创建子节点。请改用.childByAutoIduid

您应该像这样创建用户节点:

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)!
}

希望有所帮助