Firebase refFromURL不是函数 [英] Firebase refFromURL is not a function
问题描述
我正在尝试使用HTML表中的Javascript从Firebase存储中获取一些图像,但是我一直收到此错误:
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
///从Google Cloud Storage URI创建引用 var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg')
// Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg')
javascript代码也从Firebase数据库获取数据,并且数据库中的每个子级都以 gs://xxxxxxx的形式包含指向Firebase存储的URL(在db中定义为imageUrl
). ,我想在表格中使用.换句话说,我想从数据库中获取文本,从存储中获取图像,但是我一直收到"not a function"错误.我有以下代码:
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>");
});
以及包含Firebase信息的HTML文件:
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>
推荐答案
您基本上是这样做的:
var storage = firebase.storage().ref();
哪个可以给您 Reference
Which gives you a Reference
.
然后您致电:
storage.refFromURL('url_ul')
But refFromURL
is only defined on the root Storage
object.
因此,您需要:
var storage = firebase.storage();
storage.refFromURL('url_ul')
这篇关于Firebase refFromURL不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!