未被捕获的异常' InvalidPathValidation',原因:'(child :)必须是非空字符串且不包含'。' '#' ' $' ' ['或者']''

时间:2018-04-30 02:24:08

标签: ios swift firebase firebase-realtime-database

我正在对我的应用进行身份验证,但收到错误' invalidPathValidation'当我点击注册按钮时。

根据我的研究,我认为这与Firebase不接受的电子邮件中的字符有关。

@IBAction func signUpButtonPressed(_ sender: Any) {
    guard emailAddress.text != "" , password.text != ""
        else {
            errorOutlet.isHidden = false
            return errorOutlet.text = "Email or Password field is empty."
    }

    if password.text != "" , emailAddress.text != "" {
        Auth.auth().createUser(withEmail: emailAddress.text! , password: password.text!, completion: { (user, error) in
            if let error = error {
                print(error.localizedDescription)
                self.errorOutlet.isHidden = false
                return self.errorOutlet.text = "couldn't create account"
            }

            if let user = user {
                let changeRequest = Auth.auth().currentUser!.createProfileChangeRequest()
                changeRequest.displayName = self.firstName.text!
                changeRequest.commitChanges(completion: nil)

                let imageRef = self.userStorage.child("\(user.uid).jpg")
                let data = UIImageJPEGRepresentation(self.profileImage.image!, 0.6)
                let uploadTask = imageRef.putData(data! , metadata: nil, completion: { (metadata, err) in
                    if err != nil {
                        print(err!.localizedDescription)

                        // an err occured
                        self.errorOutlet.isHidden = false
                        return self.errorOutlet.text = "error"
                    }

                    imageRef.downloadURL(completion: { (url , er) in
                        if er != nil{
                            print(er!.localizedDescription)

                            // an error occured
                            self.errorOutlet.text = "error"
                            return  self.errorOutlet.isHidden = false

                        }

                        if let url = url {
                            let userInfo : [String : Any] = ["uid" : user.uid ,
                                                             "first Name" : self.firstName.text! ,
                                                             "last name" : self.surName.text! ,
                                                             "urlToImage" :url.absoluteString]

                            self.ref.child("users").child("user.uid").setValue(userInfo)

                            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "subscribe")
                            self.present(vc, animated: true, completion: nil)
                        }
                    })
                })

                uploadTask.resume()
            }
        })
    }
}

1 个答案:

答案 0 :(得分:1)

这条线似乎有问题:

self.ref.child("users").child("user.uid").setValue(userInfo)

你正在治疗孩子" user.uid" as string(包括有问题的"。",不能用作密钥)。

相反,请使用userInfo.uid,而不使用""。