gpt4 book ai didi

swift ,火力地堡 : No datas in realtime database after creating user

转载 作者:可可西里 更新时间:2023-11-01 01:56:10 25 4
gpt4 key购买 nike

使用 Xcode 10.1、Swift 4.2 和 Firebase ##

使用以下代码将数据上传到 firebase 后,我在实时数据库中看不到数据:

static func createUser(username: String, email: String, password: String, imageData: Data, onSuccess: @escaping () -> Void, onError: @escaping (_ error: String?) -> Void) {

Auth.auth().createUser(withEmail: email, password: password) { (data, error) in
if let err = error {
onError(err.localizedDescription)
return
}
// User erfolgreich erstellt
guard let uid = data?.user.uid else { return }
self.uploadUserData(uid: uid, username: username, email: email, imageData: imageData, onSuccess: onSuccess)
}
}

static func uploadUserData(uid: String, username: String, email: String, imageData: Data, onSuccess: @escaping () -> Void) {


let storageRef = Storage.storage().reference().child("profile_image").child(uid)

storageRef.putData(imageData, metadata: nil) { (metadata, error) in
if error != nil {
return
}
}

storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print(error!.localizedDescription)
return
}

let profilImageURL = url?.absoluteString


let ref = Database.database().reference().child("users").child(uid)
ref.setValue(["username" : username, "email" : email, "profileImageURL": profilImageURL ?? "Kein Bild vorhanden"])

})
onSuccess()
}

firebase 中的设置:

The settings in firebase

应该以正确的方式工作。我已经查看了 firebase 文档,但没有找到更多信息。

最佳答案

图像是否上传到云存储?如果是,但下载 URL 没有写入数据库,我猜是因为在生成下载 URL 之前数据没有上传。由于上传函数是异步的,因此应该从闭包中调用 downloadURL 函数。

static func uploadUserData(uid: String, username: String, email: String, imageData: Data, onSuccess: @escaping () -> Void) {

let storageRef = Storage.storage().reference().child("profile_image").child(uid)

storageRef.putData(imageData, metadata: nil) { (metadata, error) in
if error != nil {
return
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
onSuccess()
}
}

关于 swift ,火力地堡 : No datas in realtime database after creating user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53230887/

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