swift - 类型 'AuthDataResult' 的值没有成员 ‘uid’

标签 swift firebase firebase-authentication

我正在尝试在 Firebase 身份验证中访问用户的 uid。我在我的代码中创建了一个 createUser 完成 block ,在该 block 的末尾我想检查我命名为 firUser 的用户。当我尝试在我的用户中添加 firUser.uid 时,我收到错误消息

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

下面是我编写的代码的副本,希望有人能帮助我。

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)
         }
        })
      }
    })
  }
})

最佳答案

根据guide , 当使用 .createUser,

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 访问用户。

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

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
    }
  })
})

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

相关文章:

swift - 将 View 作为参数传递给按钮,将其触发为模态

swift - 按下时 NSButton 变为灰色

swift - 转换会删除 Swift 中所有级别的 Optional 吗?

ios - 如何在 Swift 中使用新的 Firebase 3.2.1 向我的 Firebase 应用程序指定提供程序(Facebook、Google)的用户进行身份验证

swift - Firebase:如何获取 User.uid?

swift - 在 Firebase 中保存用户名 - Swift

C typedef 之间的 Swift 隐式类型转换

javascript - 无法在 Firebase 实时数据库中实现分页

android - 无法解析 : firebase-common

python - 在 python admin sdk 中处理 Firebase 错误