gpt4 book ai didi

javascript - 如何从 Firebase 存储获取 DownloadURL

转载 作者:行者123 更新时间:2023-11-28 14:32:42 26 4
gpt4 key购买 nike

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

这是错误:

firebase.js:1 Uncaught (in promise) Error: Reference.push failed: first argument contains undefined in property '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');
});

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

提前致谢。

最佳答案

下载 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 时非常常见的模式:它们都是异步的,您的代码必须处理这些问题。

有关此内容的更多信息,请参阅:

关于javascript - 如何从 Firebase 存储获取 DownloadURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50883632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com