Simplemente estoy tratando de probar la carga de imágenes y quiero mostrar la imagen cargada. Mi código se ve así:
function uploadFile(files){ const storageRef = firebase.storage().ref(); //this references the firebase storage const horseRef = storageRef.child("horse.jpg"); const file = files.item(0); //will return a list so lets take the first item const task = horseRef.put(file); //to upload the file we call the put file console.log(task); task.then(snapshot => { //returns a buncha data including a snapshot url const url = snapshot.downloadURL document.getElementById("upload").setAttribute("src", url) }); }Pero snapshot.downloadURL devuelve un valor indefinido. ¿Me puedes ayudar?
No hay ninguna propiedad downloadURL en la instantánea que sea un UploadTaskSnapshot . Debe usar el método getDownloadURL() en la referencia de almacenamiento para obtener la URL. Intente refactorizar el código como se muestra a continuación:
task.then(async (snapshot) => { const url = await snapshot.ref.getDownloadUrl() document.getElementById("upload").setAttribute("src", url) });