React Native 如何在firebase存储中上传图像并更新firestore中的链接

vqlkdk9b  于 2023-03-24  发布在  React
关注(0)|答案(1)|浏览(121)

我试图上传一个图像到Firebase存储和下载的URL,并更新到Firestore,但我不能上传。

const uploadData = async () => {
    if (loading) return;

    setLoading(true);

    // Upload the image blob to Firebase Storage
    const response = await fetch(image.uri);
    const blob = await response.blob();
    console.log(blob);
    const storageRef = ref(storage, `users/${uid}/profile-image`);
    const uploadTask = uploadBytes(storageRef, blob);

    // Get the download URL of the uploaded image
    let downloadURL;
    try {
      const snapshot = await uploadTask;
      downloadURL = await getDownloadURL(snapshot.ref);
    } catch (error) {
      console.error(error);
      setLoading(false);
      return;
    }

    // Update the user document in Firestore with the download URL
    const userDocRef = doc(firestore, "users", uid, "userdetails");
    try {
      await updateDoc(userDocRef, {
        name: name,
        username: username,
        email: email,
        profileImageURL: downloadURL,
        timestamp: serverTimestamp(),
      });
    } catch (error) {
      console.error(error);
      setLoading(false);
      return;
    }

    setLoading(false);
  };
k7fdbhmy

k7fdbhmy1#

可以使用this blog作为参考。
这个repo使用类似的逻辑将图像上传到firebase存储。

相关问题