'AuthDataResult' has no member uid error

北慕城南 提交于 2019-12-11 04:56:01

问题


I'm creating a new user within my firebase database, and I'm trying to set some more values to the user: name, email, password, DOB, etc.

/* 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)

The error occurs within the completion block as I'm trying to access the child(user!.uid). I want to be able to set values to it such as:

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

but I can't since there must be an issue with the Callback when I'm accessing the (user) parameter. Xcode states 'AuthDataResult' has no member uid.

Any help is much appreciated.


回答1:


Since the error message talks about AuthDataResult, it seems you have to do:

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

I'd also recommend you check out the Firebase guide on creating a user.




回答2:


Roberto,

'user' in your completion handler is of type AuthDataResult? and not of type User?. However, AuthDataResult can be used to access information of a signed in user. To access the UID in your scenario above, simply apply this:

user?.user.uid

which should look like this when applied:

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

Here you are taking the AuthDataResult 'user' and accessing it's member ('user'), which is finally of type User?. Then we are simply just accessing the User type's UID like we regularly would.

Hopefully this helps.




回答3:


buddy just reference the firebase user like this;

var user: Firebase.User?

at the top of your view controller




回答4:


This is the correct form

@IBAction func signInTapped(_ sender: Any){
    if let email = emailField.text, let password = passwordField.text{
        Auth.auth().signIn(withEmail: email, password: password, completion:
            {(user,error) in
                if error == nil {
                    if let user = user {
                        self.userUid = user.user.uid
                        self.goToCreateUserVC()
                    }
                }

        });
    }
}

This is the incorrect form:

@IBAction func signInTapped(_ sender: Any){
    if let email = emailField.text, let password = passwordField.text{
        Auth.auth()?.signIn(withEmail: email, password: password, completion:
            {(user,error) in
                if error == nil {
                    if let user = user {
                        self.userUid = user.uid
                        self.goToCreateUserVC()
                    }
                }

        });
    }
}



回答5:


As most people already said, it would be something like this:

user?.user.uid


来源:https://stackoverflow.com/questions/50360186/authdataresult-has-no-member-uid-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!