Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda