Estoy tratando de cargar una imagen en Firebase Storage y guardar la URL de descarga en Firestore, pero cuando mi código se está ejecutando, .getDownloadURL() no se ejecutará. La imagen se sube, pero después de eso, no pasa nada. Mi código está abajo. He probado con varias imágenes.
const task = firebase.storage().ref('/hendingar/' + img.name).put(img) task.then((snapshot) => { console.log('Img ') snapshot.ref.getDownloadURL((url) => { console.log('Url') firebase.firestore().collection('hendingar').add({ title: title, description: description, start: start, end: end, place: infoPlace, prices: temp_arr, img: url, }).then(() => { console.log('Success') }).catch((error) => { console.error(error) }) }) })No está obteniendo ningún resultado o registro después de console.log('Img ') porque no está encadenando la promesa correctamente. Debe llamar a getDownloadURL() antes de encadenar .then() . Vea el código a continuación:
const task = firebase.storage().ref('/hendingar/' + img.name).put(img) // Uploads the file to Firebase Storage. task.then((snapshot) => { console.log('Img') // You can also put the creation of Firestore Document here. // (If you want it to create the document before getting the download URL.) // Calls the `getDownloadURL()` method. snapshot.ref.getDownloadURL() .then((url) => { // Returns the Download URL from Firebase Storage. console.log('Url') console.log(url) // You can put this either here or before getting download URL. firebase.firestore().collection('hendingar').add({ title: title, description: description, start: start, end: end, place: infoPlace, prices: temp_arr, img: url, }).then(() => { console.log('Success') }).catch((error) => { console.error(error) }) }) }) .catch((error) => { // Logs error if there's an error uploading of file. console.log(error) })Dejo algunos comentarios en el código de arriba para una mejor comprensión. Para obtener más información, puede consultar esta documentación .