Firebase 数据库用户的 profileImageUrl 未更新
Firebase Database user's profileImageUrl not updating
我想用我上传的图片存储 url 更新 firestore 数据库 属性 profileImageUrl
(存储代码工作正常,我可以看到上传的图片有自己的 URL ) 有什么想法吗?
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
myImage = image
self.mainProfilePicImage.image = image
} // where I choose image
guard let imageData = self.mainProfilePicImage.image?.jpegData(compressionQuality: 0.4) else {
return
} // compressing to jpeg
let userID = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://xxxxxxx.appspot.com/") // my firebase storage
let storageProfileRef = storageRef.child("profile").child(userID)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storageProfileRef.putData(imageData,metadata: metadata) { (storageMetadata, error) in
if error != nil {
print("error")
}
storageProfileRef.downloadURL(completion: {(url,error) in
if let metaImageUrl = url?.absoluteString { // downloading url from storage of picked image
print("meta image url: \(metaImageUrl)") // printing URL
let db = Firestore.firestore()
db.collection("users").document(userID).updateData([
"profileImageUrl": metaImageUrl,
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
})
}
主要问题出在我的 db.collection("users").document(userID)
行,我发现 "users"
文档有不同的 id,所以我更新了下面的代码:
db.collection("users")
.whereField("uid", isEqualTo: userID)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Couldnt update : \(err)")
} else {
let document = querySnapshot!.documents.first
document!.reference.updateData([
"profileImageUrl": url?.absoluteString
])
}
}
我想用我上传的图片存储 url 更新 firestore 数据库 属性 profileImageUrl
(存储代码工作正常,我可以看到上传的图片有自己的 URL ) 有什么想法吗?
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
myImage = image
self.mainProfilePicImage.image = image
} // where I choose image
guard let imageData = self.mainProfilePicImage.image?.jpegData(compressionQuality: 0.4) else {
return
} // compressing to jpeg
let userID = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://xxxxxxx.appspot.com/") // my firebase storage
let storageProfileRef = storageRef.child("profile").child(userID)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storageProfileRef.putData(imageData,metadata: metadata) { (storageMetadata, error) in
if error != nil {
print("error")
}
storageProfileRef.downloadURL(completion: {(url,error) in
if let metaImageUrl = url?.absoluteString { // downloading url from storage of picked image
print("meta image url: \(metaImageUrl)") // printing URL
let db = Firestore.firestore()
db.collection("users").document(userID).updateData([
"profileImageUrl": metaImageUrl,
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
})
}
主要问题出在我的 db.collection("users").document(userID)
行,我发现 "users"
文档有不同的 id,所以我更新了下面的代码:
db.collection("users")
.whereField("uid", isEqualTo: userID)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Couldnt update : \(err)")
} else {
let document = querySnapshot!.documents.first
document!.reference.updateData([
"profileImageUrl": url?.absoluteString
])
}
}