So I have a loop which is displaying the user data as expected (this works fine):
database.once("value", function(snapshot){
snapshot.forEach(snap => {
var r = document.getElementById('userTable').insertRow()
var age = r.insertCell(0).innerHTML = snap.val().key;
var aboutme = r.insertCell(1).innerHTML = snap.val().photourl;
})
The user image is stored in firebase storage like so (clicking into the folder which contains the image):
My user table looks like this:
With all the associated fields linked to that user under that and the photo_url holds the image path like:
user_photos/07KSLIkK2/D41FB90B444D-9979-22F86D69883F.jpg
Ive tried looking online but most of the answers or solutions are for android. Im pretty new to firebase.
So the question is how can i display the associated image from firebase for the user in the html table?
Thanks
Also, using:
snap.val().key;
does not show that random generated number by firebase. just shows as undefined.
UPDATE
was having some difficulties so just for testing purposes i split the code up like so:
database.once("value", function(snapshot){
snapshot.forEach(snap => {
var storageRef = firebase.storage().ref();
var temp_photo = storageRef.child('ref_name/mas.jpg')
console.log(temp_photo);
temp_photo.getDownloadURL().then(downloadURL => {
var Photo_url = document.getElementById('images').innerHTML = downloadURL;
})
})
})
In my HTML i have the following:
<div id="images" class=""></div>
what this should do is display that one images for each set of data it pulls. Below are console logs (which are repeated for each set of data its pulling)
authWrapper: Qe
app_: T {firebase_: Object, isDeleted_: false, services_: Object, tokenListeners_: Array, analyticsEventRequests_: [], …}
bucket_: "dbucketname"
deleted_: false
maxOperationRetryTime_: 120000
maxUploadRetryTime_: 600000
pool_: G {createXhrIo: function}
requestMaker_: function(e,t,r)
requestMap_: Ze {map: Map, id: -9007454433453423, addRequest: function, clear: function}
service_: nt {bucket_: J, authWrapper_: Qe, app_: T, internals_: it, ref: function, …}
storageRefMaker_: function(e,t)
Qe Prototype
location: J
bucket: "dbucketname"
path_: "ref_name/mas.jpg"
In the div it displays 1 row of the following:
UPDATE 2
So it got it to work like so
database.once("value", function(snapshot){
snapshot.forEach(snap => {
var storageRef = firebase.storage().ref();
var temp_photo = storageRef.child('ref_name/mas.jpg')
console.log(temp_photo);
temp_photo.getDownloadURL().then(downloadURL => {
var x = document.createElement("IMG");
x.setAttribute("src", downloadURL);
document.body.appendChild(x);
})
})
})
its looping around each user and displaying the picture as expected.
But when i try to move this logic to put this image into a table, i get the following error:
IndexSizeError: The index is not in the allowed range.
this is what my table insert code looks like:
var occupant = r.insertCell(16).innerHTML = snap.val().occupation;
var storageRef = firebase.storage().ref();
var temp_photo = storageRef.child('ref_name/mas.jpg')
temp_photo.getDownloadURL().then(downloadURL => {
var x = document.createElement("IMG");
x.setAttribute("src", downloadURL);
r.insertCell(17).appendChild(x)
})
var you_smoke = r.insertCell(18).innerHTML = snap.val().smoke;
UPDATE 3 - SOLUTION
This is what worked for me:
var storageRef = firebase.storage().ref();
var temp_photo = storageRef.child('ref_name/mas.jpg')
var y = r.insertCell(17).innerHTML = snap.val().photo_url;
temp_photo.getDownloadURL().then(downloadURL => {
var y = r.insertCell(17).innerHTML = '<img src="'+downloadURL+'" width="20" height="20" />';
})