Firebase提供错误:无法读取未定义的属性“ ref”

时间:2018-07-14 08:12:10

标签: javascript firebase firebase-realtime-database firebase-storage

我想将照片上传到Firebase存储中,并且一旦上传了downloadURL即可在控制台上打印。 完美地上传了照片,没有任何问题,但是在控制台中而不是在downloadURL上,未打印出来并打印了undefined并抛出错误,提示“ TypeError:无法读取undefined的属性'ref'”。

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 个答案:

答案 0 :(得分:2)

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