类型"AuthDataResult"的值没有成员"uid" [英] Value of type 'AuthDataResult' has no member ‘uid’

查看:51
本文介绍了类型"AuthDataResult"的值没有成员"uid"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Firebase身份验证中访问用户的uid.我在代码中创建了一个createUser完成块,并在该块的末尾要检查我是否将其命名为firUser的用户.当我尝试在用户中添加firUser.uid时,出现错误消息

I am trying to access a user's uid in Firebase Authentication. I created a createUser completion block in my code and at the end of the block I want to check for the user in which I named firUser. When I try to add firUser.uid in my User I get the error message

类型为'AuthDataResult'的值没有成员'uid'"

"Value of type 'AuthDataResult' has no member ‘uid’"

下面是我编写的代码的副本,希望有人可以帮助我.

Below is a copy of the code I wrote hopefully some one can help me.

Auth.auth().createUser(withEmail: email, password: password, completion: { (firUser, error) in
  if error != nil {
     // report error
  } else if let firUser = firUser {
    let newUser = User(uid: firUser.uid, username: username, fullName: fullName, bio: "", website: "", follows: [], followedBy: [], profileImage: self.profileImage)
    newUser.save(completion: { (error) in
      if error != nil {
        // report
      } else {
        // Login User
        Auth.auth().signIn(withEmail: email, password: password, completion: { (firUser, error) in
          if let error = error {
           // report error
           print(error)
         } else {
           self.dismiss(animated: true, completion: nil)
         }
        })
      }
    })
  }
})

推荐答案

根据

如果成功创建了新帐户,则该用户已登录, 您可以从结果对象中获取用户的帐户数据 传递给回调方法.

If the new account was successfully created, the user is signed in, and you can get the user's account data from the result object that's passed to the callback method.

请注意,在示例中,您返回的是authResult,而不是User对象. authResult包含一些信息,包括用户.您可以使用authResult.user到达用户.

Notice in the sample, you get back authResult, not a User object. authResult contains some information, including the User. You can get to the User using authResult.user.

此外,在调用方法时,如果成功,则用户已经登录,因此没有理由再次登录.我将示例中的参数名称更改为authResult,以帮助消除一些混淆.

In addition, when calling the method, if successful, the user is already signed in, so there's no reason to sign them in again. I changed the parameter name to authResult from the sample to help eliminate some of the confusion.

Auth.auth().createUser(withEmail: email, password: password, completion: { authResult, error in
  if let error = error {
     // report error
     return
  } 
  guard let authResult = authResult else { return }
  let firUser = authResult.user
  let newUser = User(uid: firUser.uid, username: username, fullName: fullName, bio: "", website: "", follows: [], followedBy: [], profileImage: self.profileImage)
  newUser.save(completion: { (error) in
    if let error = error {
      // report
    } else {
      // not sure what you need to do here anymore since the user is already signed in
    }
  })
})

这篇关于类型"AuthDataResult"的值没有成员"uid"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆