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

246
Visualizações
Trying to upload multiple images to firestore with Nodejs (ClientSide firebase SDK)

I am trying to upload multiple images to firestore in my nodeJS server-side code. I initially implemented it with the firestore bucket API

admin.storage.bucket().upload()

I am placing the above code in a for loop.

for(let x = 0; images.length > x; x++){
  admin.storage.bucket().upload(filepath, {options}).then(val => {
    //get image download URL and add to a list
    imageUrls.push(url);

    if(images.length == x+1){
      // break out of loop and add the imagesUrls list to firestore
    }
  })
}

but what happens is that the code sometimes doesn't add all the image urls to the imageUrls list and I'll have only 1 or 2 image urls saved to firestore while in firestorage I see it uploaded all the images.

I understand that uploading takes some time and would like to know the best way to implement this as I assumed the .then() method is an async approach and would take care of any await instances.

Your response would be highly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's not clear how you get the values of filepath in the for block, but let's imagine that images is an array of fully qualified paths to the images you wish to upload to the bucket.

The following should do the trick (untested):

const signedURLs = [];

const promises1 = [];
images.forEach(path => {
    promises.push(admin.storage.bucket().upload(filepath, {options}))
})

Promise.all(promises1)
.then(uploadResponsesArray => {
    const promises2 = [];
    const config = {  // See https://googleapis.dev/nodejs/storage/latest/File.html#getSignedUrl
        action: 'read',
        expires: '03-17-2025',
        //...
        };
    uploadResponsesArray.forEach(uploadResponse => {
        const file = uploadResponse[0];
        promises2.push(file.getSignedUrl(config))
    })
    return Promise.all(promises2);
})
.then(getSignedUrlResponsesArray => {
    signedURLs.push(getSignedUrlResponsesArray[0])
});

// Do whatever you want with the signedURLs array
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