CuriousPaul
CuriousPaul

Reputation: 449

Firebase refFromURL is not a function

I'm trying to get some images from Firebase storage using Javascript in an HTML table, but I keep getting this error:

storage.refFromURL is not a function

Which is weird, considering the information from here

// Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg')

The javascript code gets data from a Firebase database as well, and each child in the database contains an URL (defined in the db as imageUrl) to a Firebase storage in the form of gs://xxxxxxx, which I would like to use in a table. In other words, I would like to have the text from a database, and the image from storage, but I keep getting the "not a function" error. I have the following code:

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>");
});

And the HTML file which contains the Firebase information:

<!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>

Upvotes: 7

Views: 7983

Answers (2)

H&#233;ctor BlisS
H&#233;ctor BlisS

Reputation: 135

In Firebase V7 you can do:

import {
  getStorage,
  deleteObject,
  ref,
} from "firebase/storage";

const storage = getStorage();
const httpsReference = ref(storage, url); // this url is coming from getDownloadURL()

// Then you do whatever you want with the ref, 🍿 like remove files:
deleteObject(httpsReference);

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

You essentially do:

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

Which gives you a Reference.

Then you call:

storage.refFromURL('url_ul')

But refFromURL is only defined on the root Storage object.

So you'll want:

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

Upvotes: 8

Related Questions