【问题标题】:react-native expo : how to upload image to firebase storagereact-native expo:如何将图像上传到 Firebase 存储
【发布时间】:2021-07-31 17:03:54
【问题描述】:

我正在使用 firebase 构建应用程序。 我已经成功实现了一个功能,使用户能够将图片上传到 Firebase 存储 在这里

  const uploadImageToBucket = async () => {
    let blob;
    try {
      setUploading(true);
      blob = await getPictureBlob(image);

      const ref = await storage.ref().child(uuid.v4());
      const snapshot = await ref.put(blob);

      return await snapshot.ref.getDownloadURL();
    } catch (e) {
      alert(e.message);
    } finally {
      blob.close();
      setUploading(false);
    }
  };

问题是我希望根据特定用户上传图片,并且我想将该图片设置为用户个人资料图片。请提供任何建议!!

【问题讨论】:

  • 您知道该用户的 UID,并且您是否尝试在该用户的 Firebase 身份验证中设置 photoURL
  • 没错。确定我知道用户 uid,因为用户已经注册了
  • 只是为了确认您所说的“特定用户”是什么意思?您正在尝试在上传图片后从当前登录的用户设置 photoURL 吗?
  • 嗯,这就是我的意思

标签: firebase react-native expo react-native-navigation react-native-firebase


【解决方案1】:

这里是用户注册功能

  const handleSignUp = () => {
    setErrortext("");
    if (!FullName) return alert("Please fill Name");
    if (!Email) return alert("Please fill Email");
    if (!Password) return alert("Please fill Address");
    setIsLogged(true);
    firebase
      .auth()
      .createUserWithEmailAndPassword(Email, Password)
      .then((user) => {
        alert("Registration Successful. Please Login to proceed");
        console.log(user);
        if (user) {
          firebase
            .auth()
            .currentUser.updateProfile({
              displayName: FullName,
            })
            .then(() => navigation.replace("Log_In"))
            .then(() => {
              firebase.auth().onAuthStateChanged((userData) => {
                setuserData(userData);
              });
            })
            .catch((error) => {
              console.log(error);
              setErrortext(error);
            });
        }
      })
      .catch((error) => {
        if (error.code === "auth/email-already-in-use") {
          setErrortext("That email address is already in use!");
          setIsLogged(false);
        } else {
          setErrortext(error.message);
          setIsLogged(false);
        }
      });
  };

【讨论】:

  • 如果您要提供有关问题的其他详细信息,那么您应该编辑问题并添加它们,而不是添加详细信息作为答案。
【解决方案2】:

您可以简单地使用updateProfile 方法更新当前登录用户的photoURL,并将其设置为刚刚请求的下载URL。 uploadImageToBucket 函数返回那个 URL,所以你可以试试这个:

uploadImageToBucket().then(async (photoURL) => {
  const user = firebase.auth().currentUser
  await user.updateProfile({ photoURL })
  console.log("Photo URL updated")
}) 

【讨论】:

  • 我没有明白你的意思。你能解释一下吗?
  • @houcinolmostaf 你能复制我的代码吗?无论你在哪里调用上传图片功能,在此之后调用 updateProfile 方法
  • 我没有明白你的意思。你能解释一下吗?或者只是将你给我的示例与我在上面的问题中提供的代码链接起来
猜你喜欢
  • 2020-03-06
  • 2020-06-30
  • 1970-01-01
  • 2021-06-07
  • 2020-04-23
  • 1970-01-01
  • 2021-11-30
  • 2020-03-23
  • 2019-05-20
相关资源
最近更新 更多