Javascript 如何在firebase storage ref中使用变量
我构建了一个函数来获取指定ref的下载URL,可以在下面找到:Javascript 如何在firebase storage ref中使用变量,javascript,firebase,firebase-storage,Javascript,Firebase,Firebase Storage,我构建了一个函数来获取指定ref的下载URL,可以在下面找到: function download_file(val) { //val = Uploads/Files/swxhlhae-tech-logo.png inside of the function because i have it logged.. console.log(val) var storage = firebase.storage(); const value = 'Uploads/Files/' + val
function download_file(val) {
//val = Uploads/Files/swxhlhae-tech-logo.png inside of the function because i have it logged..
console.log(val)
var storage = firebase.storage();
const value = 'Uploads/Files/' + val
storage.ref('Uploads/Files/swxhlhae-tech-logo.png').getDownloadURL().then(function(url) {
console.log(url)
}).catch(function(error) {
console.log(error)
});
}
编辑:
var-databaseRef=firebase.database().ref('Uploads/Files/');
var-rowIndex=1;
databaseRef.once('value',函数(快照){
snapshot.forEach(函数(childSnapshot){
var childKey=childSnapshot.key;
var childData=childSnapshot.val();
//儿童钥匙
//childData.FirstName
var html='';
html+='';
html+='';
html+=''+childData.FileName+'
';
html+=''+'上载:'+childData.upload+'
';
html+='';
html+='';
html+='';
html+='';
html+='';
html+='';
html+=' ';
document.getElementById('files').innerHTML+=html;
});
});
编辑:上面的代码生成html,我这样做是为了在按钮上生成一个标题,当单击时,它获取按钮标题值,然后它将(val)变量传递给上面的函数,然后从那里开始
基本上,当我使用上面的例子时,它工作得很好。。。但当我使用一个名为(value)的变量时,它告诉我有一个404文件不存在。。。但是当我查看(value)变量和我在函数中输入的原始ref时。。。有完全相同的为什么它不工作?为什么我不能使用该变量?问题在于,当用户单击元素时,
onclick=“download\u file(this.title)”
中的this.tile
将被计算,此时this
是一个不同的对象。解决此问题的最简单方法是在呈现HTML时立即将正确的值注入childData.DownloadName
:
html += `<a href="#" onclick="download_file('${childData.DownloadName}')" title="${childData.DownloadName}"><span class="task-cat blue">Download</span></a>`;
html+=`;
我将您的示例粘贴在我的错误点,根据chromes log,代码行中充满了错误?但我明白您的意思很高兴听到这是有意义的。我更新以修复字符串嵌套问题。如果我的答案有用,请单击向上投票按钮(▲) 如果它回答了您的问题,请单击复选标记(✓) 接受它。这样其他人就知道你得到了(足够的)帮助。也看到了非常感谢!!我真的非常感谢!你能解释一下@Aqdas这里的链接是如何应用的吗?这个问题似乎是关于HTML中的This
值,而链接是关于异步加载的。虽然可能有一些相似之处(因为它们都是关于一段代码的求值时间),我认为它们没有那么相似,更不用说重复了。
html += `<a href="#" onclick="download_file('${childData.DownloadName}')" title="${childData.DownloadName}"><span class="task-cat blue">Download</span></a>`;