更新photoURL Firebase v9web,反应Javascript



我正在学习一个使用javascript和react创建firebaseweb应用程序的Udemy firebaseUdemy教程。课程材料有点过时。我想知道如何更改用户的照片URL-他们用电子邮件和密码登录,我有一个单独的钩子来处理注册信息。

以下是Udemy课程提供的更新用户照片URL的代码-有人知道我会如何在Firebase v9中写这篇文章吗?非常感谢。

// upload user thumbnail
const uploadPath = `thumbnails/${res.user.uid}/${thumbnail.name}`
const img = await projectStorage.ref(uploadPath).put(thumbnail)
const imgUrl = await img.ref.getDownloadURL()
// add display AND PHOTO_URL name to user
await res.user.updateProfile({ displayName, photoURL: imgUrl })

从v9之前的版本重写到v9通常非常简单,尤其是如果您按照升级指南进行操作,或者在上传文件的文档中比较v8和v9示例。

在你的代码中,这个v8代码:

const uploadPath = `thumbnails/${res.user.uid}/${thumbnail.name}`
const img = await projectStorage.ref(uploadPath).put(thumbnail)
const imgUrl = await img.ref.getDownloadURL()
// add display AND PHOTO_URL name to user
await res.user.updateProfile({ displayName, photoURL: imgUrl })

在v9及以上版本中变成这样:

const uploadPath = `thumbnails/${res.user.uid}/${thumbnail.name}`
const img = await uploadBytes(ref(projectStorage, uploadPath, thumbnail))
const imgUrl = await getDownloadUrl(img.ref)
// add display AND PHOTO_URL name to user
await updateProfile(res.user, { displayName, photoURL: imgUrl })

如果你仔细观察,你会发现大部分的变化只是将名称空间的object.operation()方法调用移动到模块化/函数化的operation(object)形式。除此之外,这里最大的区别是put现在被称为uploadBytes

最新更新