Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda