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

151
Views
Subir varios archivos a firebase y recuperar su URL

puedo cargar los archivos pero no sé cómo puedo obtener los enlaces URL. Tengo el const fileArr que recibirá cada archivo, pero no sé cómo puedo acceder a él.

 const fileUpload = (name: string) => { let fileArr: { name: string; url: string; type: string }[] = []; let objectArr: any[] = []; Object.entries(file).forEach(([key, value]) => objectArr.push(value)); if (file !== null) { const res = new Promise((resolve, reject) => { objectArr.forEach((item: any) => { const fileRef = ref(storage, `${name}/${item.name}`); uploadBytes(fileRef, item).then(() => { getDownloadURL(fileRef).then((url) => { fileArr.push({ name: item.name, url, type: item.type }); }); }); if (!fileArr) { reject("error"); } else { resolve(fileArr); } }); }); res .then((value) => { return value; }) .catch((error) => { console.log(error); }); } };

Estoy llamando a esta función así

 const letsTry = () => { const result = fileUpload("anyname"); console.log(result); };

La consola simplemente registra 'indefinido'. Cómo debería hacer esto?

EDITAR Entonces, en la expresión 'then catch', antes de que regrese, pongo un archivo console.log. Está registrando 'error', pero las imágenes se están cargando.

 res .then((value) => { console.log(value); return value; }) .catch((error) => { console.log(error); return "error"; });
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

El problema está en esta línea:

 if (!fileArr) {

Dado que inicializa fileArray como let fileArr: { name: string; url: string; type: string }[] = []; , siempre tiene un valor, incluso si es una matriz vacía.

Y dado que tanto uploadBytes como getDownloadURL son operaciones asincrónicas, resuelve la promesa antes de que se complete cualquier carga.

Lo que querrá hacer es verificar al final de obtener una URL de descarga si ahora tiene todas las URL de descarga que espera.

Puede hacer esto con Promise.all() , pero también puede simplemente comparar la cantidad de archivos originales que está cargando con la cantidad de URL de descarga que tiene, cada vez que obtiene una nueva URL de descarga. Eso debería ser algo como esto:

 objectArr.forEach((item: any) => { const fileRef = ref(storage, `${name}/${item.name}`); uploadBytes(fileRef, item).then(() => { getDownloadURL(fileRef).then((url) => { fileArr.push({ name: item.name, url, type: item.type }); if (fileArr.length === objectArr.length) { resolve(fileArr); } }); }); });
about 4 years ago · Santiago Gelvez Report

0

La cuarta línea: Object.entries(file).forEach(([key, value]) => objectArr.push(value)); Creo que el file variables no está definido en la línea 4.

about 4 years ago · Santiago Gelvez 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!