reactjs - 如何在 Firebase 9 中上传多个图像 - React Js

标签 reactjs google-cloud-firestore next.js firebase-storage

我有一组图像正在尝试上传到 firebase (firestore),但是当我尝试上传时,它会抛出错误 dataUrl.match 不是函数。这是我的代码:

表单.js

import { db, storage } from "../../firebase";
import {
  addDoc,
  collection,
  doc,
  serverTimestamp,
  updateDoc,
} from "@firebase/firestore";
import { getDownloadURL, ref, uploadString } from "@firebase/storage";
import { useSelector } from "react-redux";

function Form() {
  const Images = useSelector((state) => state.draggedImages.images);

  const imageTarget = Images.length - 1;

  const SendPost = async () => {
    const docRef = await addDoc(collection(db, "Post-Ad"), {
      id: session.user.uid,
      username: session.user.name,
      ProductName: name,
      AdTitle: title,
      AdDescription: description,

      timestamp: serverTimestamp(),
    }).then(() => console.log("sent to firestore"));

    const imageRef = ref(storage, `Post-Ad/${docRef?.id}/image`);
    Images[imageTarget]?.map((Img) =>
      uploadString(imageRef, Img, "data_url").then(async () => {
        const downloadURL = await getDownloadURL(imageRef);
        await updateDoc(doc(db, "posts", docRef.id), {
          image: downloadURL,
        });
      })
    );
  };
}

表单信息已上传,但图片未上传。

 The error i'm getting

最佳答案

我似乎使用了错误的函数来上传图像,这就是为什么我得到 DataUrl.match 不是一个函数。所以我所做的就是将“uploadString”替换为“uploadBytes”,然后该错误就消失了。 这是我遵循的引用,感谢@Mousu​​miRoy:Error 400 Bad Request while Uploading Image to firebase storage in React Native

这是代码现在的样子

表单.js

import { db, storage } from "../../firebase";
import {
  addDoc,
  collection,
  doc,
  serverTimestamp,
  updateDoc,
} from "@firebase/firestore";
import { getDownloadURL, ref, uploadBytes } from "@firebase/storage";
import { useSelector } from "react-redux";

function Form() {
  const Images = useSelector((state) => state.draggedImages.images);

  const imageTarget = Images.length - 1;

  const SendPost = async () => {
    const docRef = await addDoc(collection(db, "Post-Ad"), {
      id: session.user.uid,
      username: session.user.name,
      ProductName: name,
      AdTitle: title,
      AdDescription: description,

      timestamp: serverTimestamp(),
    }).then(() => console.log("sent to firestore"));

    const imageRef = ref(storage, `Post-Ad/${docRef?.id}/image`);
    Images[imageTarget]?.map((Img) =>
      uploadBytes (imageRef, Img, "data_url").then(async () => {
        const downloadURL = await getDownloadURL(imageRef);
        await updateDoc(doc(db, "posts", docRef.id), {
          image: downloadURL,
        });
      })
    );
  };
}

关于reactjs - 如何在 Firebase 9 中上传多个图像 - React Js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70390342/

相关文章:

Firebase 您的 Cloud Firestore 数据库对于基本安全规则有不安全的规则

reactjs - 如何使用 Next.js 在组件级别导入 SCSS

azure - Next.js next-auth.js 与 Azure Active Directory B2C 重定向 URI 未注册

javascript - 类型错误 : Cannot read property 'classList' of undefined

javascript - onBlur 未在 Jest 测试用例、React App 中调用

javascript - 将 Redux Saga 与 React 结合使用时出现此错误 .. Uncaught TypeError : getPosts is not a function

express - 带有 NextJs 的 Github webhook

reactjs - 使用 js 源映射中的 React 组件

firebase - Firestore 标签数据模型最佳实践(数组与字典)

firebase - 在 DataTable 中用 flutter 显示 firestore 数据