Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Firebase storage, new image upload doesn't have a unique name

I am creating an app using firebase and react.js. When I reload the app, only the very last image shows up, when instead all of them should be loaded on the first page. Here is my handleUpload function where the issue lies (Updated):

    const handleUpload = () => {
    //const storageRef = ref(storage, `images/${image.name}`);
    const randomId = doc(collection(db, "temp")).id;
    const storageRef = ref(storage, `images/${randomId}`);
    const uploadTask = uploadBytesResumable(storageRef, image);

    uploadTask.on(
        "state_changed",
        (snapshot) => {
            //Progress function ... (shows the load bar)
            const progress = Math.round(
                (snapshot.bytesTransferred / snapshot.totalBytes) * 100
            );
            setProgress(progress);
        },
        (error) => {
            //Error Function...
            console.log(error);
            alert(error.message);
        },
        async () => {
            //complete function
                const url = await getDownloadURL(storageRef);
                const docRef = await addDoc(collection(db, "posts"), {
                    imageURL: url, 
                    caption: caption, 
                    username: username, 
                    timestamp: serverTimestamp()
                });

                console.log("THE APP HAS POSTED");
                setProgress(0);
                setCaption('');
                setImage(null);
                setUrl('');
            }
        )
            

    }

Here is my storage console after restarting and posting 2 photos from an iPhone: Firebase storage console

There should be 2 photos there, but only the latest one shows up, with the name ${image.name}, which I don't understand because I have that line commented out.

Note: this issue only occurs when someone uploads on a phone since there are no file names on a phone, just a picture from a camera roll. Is there a way to fix this that anyone can think of?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Firebase storage doesn't generate a random ID for new files. You must specify then file name while uploading it. You can use a package like UUID or even Firestore to generate random IDs as shown below:

const randomId = doc(collection(db, "temp")).id
const storageRef = ref(storage, `images/${randomId}`);

Also make sure you add the file extension at the end of filename.

Are you trying to upload image with same name? In that case it'll overwrite the existing image with that name.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!