【问题标题】:How do i use a variable in firebase storage ref如何在 Firebase 存储参考中使用变量
【发布时间】:2021-03-17 14:19:57
【问题描述】:

我已经构建了一个函数来获取指定 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
                                            
 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', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
  var childKey = childSnapshot.key;
  var childData = childSnapshot.val();
  
  
  
  //childKey
  //childData.FirstName
  var html = '<li class="collection-item">';
    html += '<div class="row">';
    html += '<div class="col s9">';
    html += '<p class="collections-title">' + childData.FileName + '</p>';
    html += '<p class="collections-content">' + 'Uploaded: ' + childData.Uploaded + '</p>';
    html += '</div>';
    html += '<div class="col s3">';
    html += '<a href="#" onclick="download_file(this.title)" title=" '+ childData.DownloadName + '">' + '<span class="task-cat blue">Download</span></a>';
    html += '<a href="#" title="' + childKey + '" onclick="delete_file(this.title)"><span id="' + childKey + '" class="task-cat red">Delete</span></a>';
    
    html += '</div>';
    html += '</div>';
    html += '</li>';
    

    document.getElementById('files').innerHTML += html;
    
    
  
    });
  });

编辑:上面的这段代码生成html,我做了它,所以它在按钮上制作了一个标题,当点击它时,它会获取按钮的标题值,然后它将(val)变量传递给上面的那个函数,然后去从那里

基本上,当我使用上面的示例时,它工作得很好……但是当我使用一个名为 (value) 的变量时,它告诉我有一个 404 文件不存在……但是当我查看 (value) 时) 变量和我输入到函数中的原始 ref... 完全相同,为什么它不起作用?为什么我不能使用变量?

【问题讨论】:

  • 你能解释一下链接在@Aqdas 是如何应用的吗?这个问题似乎是关于 HTML 中的 this 值,而链接是关于异步加载的。虽然可能有一些相似之处(因为它们都是关于何时一段代码被评估),但我不认为它们都那么相似,更不用说重复了。

标签: javascript firebase firebase-storage


【解决方案1】:

问题是onclick="download_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>`;

【讨论】:

  • 我将您的示例粘贴在我的错误位置,并且根据 chrome 日志,那行代码中充满了错误?
  • 但我明白你的意思
  • 非常感谢!!我真的很感激!
猜你喜欢
  • 2011-02-16
  • 1970-01-01
  • 2016-10-17
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2020-07-26
  • 2020-11-01
相关资源
最近更新 更多