【发布时间】: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