Novato aquí, estoy tratando de cargar una imagen en el almacenamiento de Firebase y luego obtener la URL de descarga. Luego agregaré esa URL como datos para guardar en firestore. Puedo cargar correctamente y obtener la referencia de la imagen, pero el problema está en getDownloadURL() . Tengo problemas para formatear la promesa. Lo he intentado de varias maneras, pero parece que no puedo resolverlo.
TypeError: ref._throwIfRoot is not a function at getDownloadURL$1 (index.esm2017.js?f61e:2956:1) at getDownloadURL (index.esm2017.js?f61e:3412:1) at eval (cloudStorageFunctions.js?05e1:17:34) const productImagesRef = ref(storage, 'images/product-images'); function getImageURL(fileName, file) { const filePath = `${productImagesRef}/${fileName}`; const imageRef = ref(storage, filePath); return uploadBytes(imageRef, file) .then(snapshot => { console.log(`File: ${fileName} uploaded successfully.`); return snapshot.metadata.fullPath; }) .then(path => { console.log(typeof path, path); return getDownloadURL(path); }) .then(src => { console.log(src); return src; }) .catch(e => { console.error(e); }) }Los datos se envían a través de un formulario:
async function handleSubmit() { let src = await getImageURL(imageName, imageFile); setImageSrc(src); const data = { id: id, imageSrc: imageSrc, description: description, category: category, discount: discount, discountPrice: discountPrice, price: price, sizes: sizes, totalQuantity: quantity, type: type, } console.log(data); if(imageSrc) await addProducts(data); setImageFile(''); setImageName(''); setImageSrc(''); setDescription(''); setCategory(''); setDiscount(false); setDiscountPrice(0); setPrice(''); setSizes([]); setQuantity(''); setType([]); setDisplay('none') alert('Added'); }getDownloadURL toma una referencia de almacenamiento como la que le pasaste a uplaodBytes .
return getDownloadURL(imagePath);No me queda claro lo que estás pasando actualmente en su lugar. Sugiero revisar la documentación para ver un ejemplo.
La función uploadBytes devuelve Promise<UploadResult> . Está pasando ese resultado a la función getDownloadURL , pero eso espera una StorageReference .
Solución sencilla:
return uploadBytes(imageRef, file) .then(snapshot => { return snapshot.metadata.fullPath; }) .then(path => { return getDownloadURL(imageRef); })