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>
Firebase Storage: Invalid argument in 'refFromURL' at index 0: Expected full URL but got a child path, use ref instead.
Theurl_ul
read gs://xxxxx. I tried that variable in the table. Any idea what may cause it? – Bezant