【问题标题】:Data is not being inserted under the node of user UID in Firebase in Swift 3数据未插入到 Swift 3 中 Firebase 中用户 UID 的节点下
【发布时间】: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


【解决方案1】:

仅供参考

这是我当前的工作代码

注册按钮

 // Create new User
            FIRAuth.auth()?.createUser(withEmail: self.tfEmail.text!, password: self.tfPassword.text!, completion: { (user, error) in

                if error == nil{ // IF NO ERROR

                    let astrContact = self.strDialCode + " " + self.tfMobileNumber.text!

                    // Dict to add user data in firebase Db
                    let aDBDict : [String : String] = ["userName": self.tfFullName.text!,
                                                       "userEmail": self.tfEmail.text!,
                                                       "userContact": astrContact,
                                                       "userCountry": self.strCode,
                                                       "userID": (user?.uid)!]


                    // Add data in DB
                    ref?.child("Customer/\(String(describing: (user?.uid)!)/userProfileDetails").setValue(aDBDict)

                    DispatchQueue.main.async(execute: { () -> Void in

                        // goto home VC
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let navController = storyboard.instantiateViewController(withIdentifier: "MainController")

                        if let window = AppDelegate.getAppDelegate().window {
                            window.rootViewController = navController
                        }
                    })
                }
                else{ // If error in creating new user
                    print("error in creating new user")
                    print(error!)
                }
            })

在 appDelegate 中

extension AppDelegate {

    class func getAppDelegate() -> AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }
}

【讨论】:

  • getAppDelegate() 未定义?
  • appdelegate 的值没有成员userdetails
  • appdelegeate 的值没有成员uID
  • 我删除了一些与您无关的代码工作。这些是 AppDelegate 中声明的 var。所以使用新的编辑代码。
  • 你知道这个变量吗?
猜你喜欢
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多