【问题标题】:How to sort firebase getDownloadURL() results with javascript?如何使用 javascript 对 firebase getDownloadURL() 结果进行排序?
【发布时间】:2018-01-04 09:16:56
【问题描述】:

此函数正在显示来自 firebase url 的图像:

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

orderByChild 的结果已排序,但 getDownloadURL() 的结果未排序。

如何在添加到我的 html 之前对 getDownloadURL() 检索到的图像进行排序?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database firebase-storage


    【解决方案1】:

    一个技巧是以正确的顺序插入 HTML:

    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 the image file
        var storageRef = firebase.storage().ref();
        var imageRef = storageRef.child(selfie.path);
    
        var li = document.createElement("li");
        ul.appendChild(li); // this ensures the <li> is in the 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;
        })
    });
    

    【讨论】:

    • 太棒了!我把你的支票寄到哪里?!我花了 8 个小时在这上面,我阅读了你的许多其他帖子,弗兰克,几乎直接给你发了消息。太感谢了!对于想知道如何颠倒顺序的好奇者:我将时间戳设置为 0 - Date.now() 并使用以下命令以相反的顺序存储图像: object.setWithPriority({...} , 0 - Date.now ()) 希望对某人有所帮助。干杯!
    • 很高兴听到这解决了您的问题,Richard,感谢您的编辑。 :-) 快乐的黑客攻击!
    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 2021-06-29
    • 1970-01-01
    • 2018-10-03
    • 2017-10-18
    • 2021-12-29
    • 2021-08-25
    • 2014-09-14
    相关资源
    最近更新 更多