简体   繁体   English

如何从Firebase存储获取DownloadURL

[英]How to get DownloadURL from Firebase Storage

I am working over the storage firebase and I am trying to get the URL and set it to a variable, unfortunately I am not able to do it, something is wrong with my code. 我正在存储Firebase上工作,我试图获取URL并将其设置为变量,但不幸的是我无法执行此操作,我的代码有问题。 Could somebody help me out? 有人可以帮我吗?

This is the error: 这是错误:

firebase.js:1 Uncaught (in promise) Error: Reference.push failed: first argument contains undefined in property 'PostUsers.ImageURL.i' firebase.js:1未捕获(承诺)错误:Reference.push失败:第一个参数包含未定义的属性'PostUsers.ImageURL.i'

This is the code: 这是代码:

console.log('Nice, The file has been successfully uploaded to the Firebase storage!');
var DownloadURL = uploadTask.snapshot.downloadURL;
var Url_File = uploadTask.snapshot.ref.getDownloadURL().then(function (URL) {
           return URL;
           });
           alert(Url_File);
                                    uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
                                        console.log('File available at', downloadURL);
                                        //Url_File = downloadURL;
                                    });
                                    //getting the publication time
                                    var dayObj = new Date();
                                    var day = dayObj.getDate();
                                    var monthNames = ["January", "February", "March", "April", "May", "June",
                                        "July", "August", "September", "October", "November", "December"
                                    ];
                                    var month = monthNames[dayObj.getMonth()]
                                    var year = dayObj.getFullYear();
                                    var hour = dayObj.getHours();
                                    var minutes = dayObj.getMinutes();
                                    var seconds = dayObj.getSeconds();
                                    var GlobalUserName = <%=Session["UserId"] %>;
                                        firebase.database().ref('PostUsers/').push({
                                            ImageURL: Url_File,
                                            userAuthor: GlobalUserName,
                                            day: day,
                                            month: month,
                                            year: year,
                                            hour: hour,
                                            minutes: minutes,
                                            seconds: seconds
                                        });
                                        //console.log('the download Url of your file is: '+ downloadURL);
                                        console.log('User post successfully added to realtime data bases');
                                    });

I have tried in many different ways, however none of them is working properly. 我已经尝试了许多不同的方法,但是它们都无法正常工作。

thanks in advance. 提前致谢。

In this bit of code: 在这段代码中:

var Url_File = uploadTask.snapshot.ref.getDownloadURL().then(function (URL) {
    return URL;
});

The download URL is in the promise callback in a variable called URL . 下载URL在名为URL的变量的promise回调中。 It's not in Url_File . 它不在Url_File Url_File is going to contain a promise returned by then() . Url_File将包含then()返回的promise。

You need to put the code that uses the download URL in the promise callback where it's first available. 您需要将使用下载URL的代码放在第一次可用的promise回调中。 The way you have it now, the code that uses the download URL is being executed before the URL is available. 您现在拥有的方式是,在URL可用之前,正在执行使用下载URL的代码。

The download URL is only available in the then callback that you get from getDownloadURL() . 下载URL仅在从getDownloadURL()获得的then回调中可用。 You can't access it outside of that. 您不能在此之外访问它。

So the solution is to move all code that needs the download URL into the callback: 因此,解决办法是将需要下载的URL 步入回调的所有代码:

uploadTask.snapshot.ref.getDownloadURL().then(function (URL) {
   //getting the publication time
    var dayObj = new Date();
    var day = dayObj.getDate();
    var monthNames = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    ];
    var month = monthNames[dayObj.getMonth()]
    var year = dayObj.getFullYear();
    var hour = dayObj.getHours();
    var minutes = dayObj.getMinutes();
    var seconds = dayObj.getSeconds();
    var GlobalUserName = <%=Session["UserId"] %>;
    firebase.database().ref('PostUsers/').push({
        ImageURL: URL,
        userAuthor: GlobalUserName,
        day: day,
        month: month,
        year: year,
        hour: hour,
        minutes: minutes,
        seconds: seconds
    });
    console.log('User post successfully added to realtime database');
});

This is a very common pattern when dealing with modern web APIs: they're all asynchronous and your code will have to deal with that. 在处理现代Web API时,这是一种非常常见的模式:它们都是异步的,因此您的代码必须对此进行处理。

For more on this see: 有关更多信息,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 firebase 存储中获取每个文件的下载 URL - Get the downloadURL for each file from firebase storage Firebase 存储 - 获取实际数据而不是 downloadURL - Firebase Storage - Get actual data instead of downloadURL 如何将 firebase storage downloadURL 保存到 firestore 集合? - How to save firebase storage downloadURL to firestore collection? 我无法从Firebase Storage(Angular / Ionic)获取图像downloadUrl - I can't get image downloadUrl from Firebase Storage (Angular/Ionic) 如何在使用 firebase 存储上传后获取调整大小的 downloadUrl(Web SDK + 调整图像扩展) - How to get the resized downloadUrl after upload with firebase storage (Web SDK + Resize images extention) 如何从此函数获取downloadURL? - How to get downloadURL from this function? I need to get the download URL from Firebase, Storage, using JavaScript, in html, The Picture gets uploaded, but I get no DownloadURL? - I need to get the download URL from Firebase, Storage , using JavaScript, in html , The Picture gets uploaded , but I get no DownloadURL? Firebase 存储不返回 downloadURL - Firebase storage don't return downloadURL Expo firebase 7.9.0 无法获取downloadUrl - Expo firebase 7.9.0 can't get downloadUrl 如何从 Firebase 存储中获取图像文件名? - How to get image filename from Firebase Storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM