0
votes

I want to upload a photo in firebase storage and once its uploaded a downloadURL to be printed on console. Photo is uploaded perfectly without any issue but in console instead of downloadURL , undefined is printed and an error thrown which states that 'TypeError: Cannot read property 'ref' of undefined'.

const file = $('#exampleInputFile').get(0).files[0]; // getting file to be uploaded 
const name = (+new Date() + '-' + file.name); // creating filename with timestamp
const task = ref.child(name).put(file, {contentType: file.type});  //setting file 

task.then( (snapshot) =>  console.log (snapshot.downloadURL))
.then(downloadURL => {
console.log(`Successfully uploaded file and got download link -
${downloadURL}`);
// once done trying to get downloadURL against this upload

})

.catch(error => {
 // Use to signal error if something goes wrong.
 console.log(`Failed to upload file and get link - ${error}`);
});
//or throw and error in console
1
console.log always returns undefined. - CertainPerformance
@CertainPerformance what about the errror i.e TypeError: Cannot read property 'ref' of undefined'. - Ghulam Ahmed
I don't know about that, because I don't see .ref anywhere in your code, but I don't have experience with firebase - CertainPerformance

1 Answers

2
votes
save() {
    this.storageref.child(this.file.name).put(this.file).then(snapshot => {
        let image = snapshot.metadata.downloadURLs[0];
            consol.log(image);
        });
    }