swift - 'AuthDataResult' 没有成员 uid 错误

标签 swift firebase firebase-authentication

我正在我的 firebase 数据库中创建一个新用户,我正在尝试为该用户设置更多值:姓名、电子邮件、密码、出生日期等。

/* Create User with Email and Password Provided */
    Auth.auth().createUser(withEmail: emailTextfield.text!, password: passwordTextField.text!, completion: { (user, error) in
        /* Handle errors that may occur while Creating user in Firebase */
        if let error = error {
            let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default) { (action: UIAlertAction!) in

            }
            alertController.addAction(OKAction)
            self.present(alertController, animated: true, completion:nil)
            self.signUpButton.isEnabled = true
            return
        } else {

            SVProgressHUD.show(withStatus: "Loading")
            let rootRef = Database.database().reference()
            let userRef = rootRef.child("users").child(user!.uid)

当我试图访问子项(用户!.uid)时,错误发生在完成 block 中。我希望能够为其设置值,例如:

rootRef.userRef.child("name").setValue(self.nameTextfield.text!)

但我不能,因为当我访问 (user) 参数时,回调一定有问题。 Xcode 声明“AuthDataResult”没有成员 uid。

非常感谢任何帮助。

最佳答案

罗伯托

完成处理程序中的“用户”是 AuthDataResult 类型?而不是用户类型?但是,AuthDataResult 可用于访问登录用户 的信息。要访问上述场景中的 UID,只需应用此:

user?.user.uid

应用后应该是这样的:

let userRef = rootRef.child("users").child(user?.user.uid)

在这里,您获取 AuthDataResult“用户”并访问它的成员(“用户”),最终类型为 User?。然后我们只是像往常一样访问用户类型的 UID。

希望这对您有所帮助。

关于swift - 'AuthDataResult' 没有成员 uid 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50360186/

相关文章:

firebase - Firebase 的云功能错误 : Forbidden

ios - 在iOS中捕获某些Firebase错误

swift - 如何将ShaderToy的texture()函数写入Metal着色语言?

ios - 当应用程序移至后台时如何使 URLSession 工作

android - 与项目 'com.android.support:support-annotations' 中的依赖项 ':app' 冲突。应用程序和测试应用程序的已解决版本不同

firebase - 如何发送推送通知 - Google 助理

firebase - 是否重复使用 FCM token ?

firebase - 未处理的异常:PlatformException(执行get时出错,PERMISSION_DENIED:权限缺失或不足。,null)

core-data - 在 Swift 中从数组值构建 NSCompoundPredicate

ios - 在执行 segue 后运行代码?