gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:51:57 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com