2

I have an array of images that I'm trying to upload to firebase (firestore) but when I try to upload, It throws the error dataUrl.match is not a function. Here's my code:

Form.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 form information is uploaded but the images are not.

 The error i'm getting

3
  • Can you share the screenshot of the full error stack? Commented Dec 20, 2021 at 9:04
  • Please take a look at this link : ibb.co/sKffydB @MousumiRoy Commented Dec 20, 2021 at 10:20
  • 1
    You may have a look at a similar Stackoverflow case. Let me know if it helps! Commented Dec 20, 2021 at 11:50

1 Answer 1

2

It seemed like I was using the wrong function to upload the image that's why I was getting the DataUrl.match is not a function. So All I did was replace the 'uploadString' with 'uploadBytes' and that error went away. Here's the reference I followed thanks to @MousumiRoy : Error 400 Bad Request while Uploading Image to firebase storage in React Native

Here's what the code now looks like

Form.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,
        });
      })
    );
  };
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good job. Well done! 👏
Does that const downloadURL = await getDownloadURL(imageRef); returns an array ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.