簡體   English   中英

如何從Firebase存儲獲取DownloadURL

[英]How to get DownloadURL from Firebase Storage

我正在存儲Firebase上工作,我試圖獲取URL並將其設置為變量,但不幸的是我無法執行此操作,我的代碼有問題。 有人可以幫我嗎?

這是錯誤:

firebase.js:1未捕獲(承諾)錯誤:Reference.push失敗:第一個參數包含未定義的屬性'PostUsers.ImageURL.i'

這是代碼:

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');
                                    });

我已經嘗試了許多不同的方法,但是它們都無法正常工作。

提前致謝。

在這段代碼中:

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

下載URL在名為URL的變量的promise回調中。 它不在Url_File Url_File將包含then()返回的promise。

您需要將使用下載URL的代碼放在第一次可用的promise回調中。 您現在擁有的方式是,在URL可用之前,正在執行使用下載URL的代碼。

下載URL僅在從getDownloadURL()獲得的then回調中可用。 您不能在此之外訪問它。

因此,解決辦法是將需要下載的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');
});

在處理現代Web API時,這是一種非常常見的模式:它們都是異步的,因此您的代碼必須對此進行處理。

有關更多信息,請參見:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM