【问题标题】:How to get DownloadURL from Firebase Storage如何从 Firebase 存储中获取 DownloadURL
【发布时间】:2018-06-15 23:01:37
【问题描述】:

我正在处理存储火力库,我正在尝试获取 URL 并将其设置为变量,不幸的是我无法做到这一点,我的代码有问题。有人可以帮帮我吗?

这是错误:

firebase.js:1 Uncaught (in promise) 错误: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');
                                    });

我尝试了很多不同的方法,但是没有一个能正常工作。

提前致谢。

【问题讨论】:

    标签: javascript firebase firebase-storage


    【解决方案1】:

    下载 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 时,这是一种非常常见的模式:它们都是异步的,您的代码将不得不处理它。

    有关更多信息,请参阅:

    【讨论】:

      【解决方案2】:

      在这段代码中:

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

      下载 URL 在一个名为 URL 的变量中的承诺回调中。它不在Url_File 中。 Url_File 将包含由 then() 返回的承诺。

      您需要将使用下载 URL 的代码放在第一次可用的 Promise 回调中。按照您现在的方式,使用下载 URL 的代码在 URL 可用之前执行。

      【讨论】:

      • 这个 var DownloadURL = uploadTask.snapshot.downloadURL; 也不起作用
      • 这不是我建议你使用的。
      • 我在回答中描述了您需要做的事情。在您已经编写的 promise 回调中使用 URL 变量。将代码直接放入该回调中。
      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 2022-01-07
      • 1970-01-01
      • 2018-04-01
      • 2016-11-02
      • 2022-01-13
      相关资源
      最近更新 更多