How to sort firebase getDownloadURL() results with javascript? -
this function displaying images firebase urls:
function updatetimeline(){ var ul = document.queryselector("#timeline ul"); ul.innerhtml = ""; var db = firebase.database().ref("phoodos/"); var list = db.orderbychild("timestamp"); list.on("child_added", function(child) { var selfie = child.val(); // retrieve image file var storageref = firebase.storage().ref(); var imageref = storageref.child(selfie.path); imageref.getdownloadurl().then(function(url){ var li = "<li><figure>"; li += "<img src='" + url + "' width='100%' alt='phoodo'>"; li += "<figcaption>by " + selfie.user + ": " + selfie.timestamp + "</figcaption>"; li += "</figure></li>"; ul.innerhtml += li; }) }); }
results of orderbychild sorted, results getdownloadurl() not sorted.
how can sort images retrieved getdownloadurl() before adding html?
one trick insert html in correct order:
var ul = document.queryselector("#timeline ul"); ul.innerhtml = ""; var db = firebase.database().ref("phoodos/"); var list = db.orderbychild("timestamp"); list.on("child_added", function(child) { var selfie = child.val(); // retrieve image file var storageref = firebase.storage().ref(); var imageref = storageref.child(selfie.path); var li = document.createelement("li"); ul.appendchild(li); // ensures <li> in right order imageref.getdownloadurl().then(function(url){ var html = "<figure>"; html += "<img src='" + url + "' width='100%' alt='phoodo'>"; html += "<figcaption>by " + selfie.user + ": " + selfie.timestamp + "</figcaption>"; html += "</figure>"; li.innerhtml = html; }) });
Comments
Post a Comment