【问题标题】:Firebase giving error : cannot read property 'ref' of undefinedFirebase 给出错误:无法读取未定义的属性“ref”
【发布时间】:2018-12-22 12:18:03
【问题描述】:

我想在 firebase 存储中上传照片,一旦上传了要在控制台上打印的下载 URL。 照片完美上传,没有任何问题,但在控制台而不是 downloadURL 中,打印 undefined 并引发错误,指出“TypeError:无法读取未定义的属性 '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

【问题讨论】:

  • console.log 总是返回 undefined
  • @CertainPerformance 错误是什么,即 TypeError: Cannot read property 'ref' of undefined'。
  • 我不知道,因为我在您的代码中的任何地方都没有看到 .ref,但我没有使用 firebase 的经验

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


【解决方案1】:
save() {
    this.storageref.child(this.file.name).put(this.file).then(snapshot => {
        let image = snapshot.metadata.downloadURLs[0];
            consol.log(image);
        });
    } 

【讨论】:

  • 请编辑您的帖子并为您的代码添加解释。
  • 从上传失败到成功的消息现在改为上传成功,但控制台中仍未定义下载链接
猜你喜欢
  • 2019-03-18
  • 1970-01-01
  • 2018-04-25
  • 2019-04-05
  • 2021-12-04
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多