Firebase refFromURL不是一个功能

时间:2018-03-28 13:55:30

标签: javascript firebase firebase-storage

我正在尝试使用HTML表格中的Javascript从Firebase存储中获取一些图片,但我一直收到此错误:

storage.refFromURL is not a function

考虑到来自here

的信息,这很奇怪
  

//从Google云端存储URI创建引用   var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg')

javascript代码也从Firebase数据库获取数据,并且数据库中的每个子项都包含一个URL(在数据库中定义为imageUrl)到 gs:/形式的Firebase存储/ xxxxxxx ,我想在表格中使用它。换句话说,我想从数据库中获取文本,从存储中获取图像,但我不断收到“非函数”错误。我有以下代码:

index.js

var rootRef = firebase.database().ref().child("Test");
var storage = firebase.storage().ref();

rootRef.on("child_added", snap => {
  var headline = snap.child("headline").val();
  var url_ul = snap.child("imageUrl").val();

  var storageRef = storage.refFromURL('url_ul').getDownloadURL().then(function(url) {
    var test = url;
    alert(url);
    document.querySelector('img').src = test;
  }).catch(function(error) {});

  $("#table_body").append("</td><td>" + headline + "</td><td><img src='test' alt='' border=3 height=100 width=100></td>");
});

包含Firebase信息的HTML文件:

<!DOCTYPE html>
<html>

<head>
  <title>Firebase Web Basics</title>
  <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,500,700" rel="stylesheet">
  <script src="https://use.fontawesome.com/939e9dd52c.js"></script>
</head>

<body>
  <div class="mainDiv" align="left">
    <h1>All users</h1>
    <table>
      <thead>
        <tr>
          <td>Headline</td>
          <td>Image</td>

        </tr>
      </thead>
      <tbody id="table_body">
      </tbody>
    </table>
  </div>

  <script src="https://www.gstatic.com/firebasejs/4.9.1/firebase.js"></script>
  <script>
    // Initialize Firebase
    var config = {
      apiKey: "AIxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      authDomain: "xxxxxxxxxxxx.firebaseapp.com",
      databaseURL: "https://xxxxxxxxxxxxx.firebaseio.com",
      projectId: "xxxxxxxxxxxxxxxx",
      storageBucket: "xxxxxxxxxxxxx.appspot.com",
      messagingSenderId: "0000000000000000"
    };
    firebase.initializeApp(config);
  </script>

  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script src="index.js"></script>
  <script src="https://www.gstatic.com/firebasejs/4.12.0/firebase-database.js"></script>

</body>

</html>

1 个答案:

答案 0 :(得分:2)

你基本上这样做:

var storage = firebase.storage().ref();

它为您提供Reference

然后你打电话:

storage.refFromURL('url_ul')

但是refFromURL is only defined on the root Storage object

所以你想要:

var storage = firebase.storage();
storage.refFromURL('url_ul')