【问题标题】:Sort data in Firebase Realtime Database in reverse chronological order [duplicate]按时间倒序对 Firebase 实时数据库中的数据进行排序 [重复]
【发布时间】:2019-04-26 10:49:07
【问题描述】:

我正在使用 firebase 进行实时聊天。但是现在我在数据排序方面遇到了问题,比如在 web whatsapp 中,我们让用户以我想要显示的方式显示最新时间。

这就是我在 firebase 中保存数据的方式:

                                var rootRef = firebase.database().ref();
                                var storesRef = rootRef.child('messages');
                                var adminref = storesRef.child(username);
                                var newStoreRef = adminref.push();
                                newStoreRef.set({
                                    "file": "",
                                    "id": senderid,
                                    "message": chatdata,
                                    "time": chattime,
                                    "timestamp" : nowTimestamp,
                                    "name": senderName
                                });

然后我像这样检索数据

   $(document).ready(function(){

        var rootRef = firebase.database().ref().child("users");

        rootRef.on('value', function(snapshot) 
        {
            snapshot.forEach(function(childSnapshot) 
                {
                    console.log(childSnapshot);
                    var name = childSnapshot.key;
                    var childData = childSnapshot.val();
                    //console.log(childData);

                    if($('#'+name).length) {
                        return;
                    } else {

        var rootRef = firebase.database().ref().child("messages").child(name);
                            var html = '';
                            var username = name ;
                            var userid = childData.id;

                            rootRef.limitToLast(1).orderByChild('timestamp').on('child_added', function(snapshot) 
                            {
                                     var name = snapshot.key;
                                     var childDatamsg = snapshot.val();
                                     $('#'+username).remove();
                                     //document.getElementById(username).remove();
                                     $("#users").prepend('<div class="row sideBar-body userchat" data-id="'+userid+'" onclick="showDiv()" id="'+username+'"><div class="col-sm-3 col-xs-3 sideBar-avatar"><div class="avatar-icon"><img src="http://chat.synetal.com/assets/user2.jpg"></div></div><div class="col-sm-9 col-xs-9 sideBar-main"><div class="row"><div class="col-sm-8 col-xs-8 sideBar-name-align" id="chat"><span class="name-meta">'+ username +'</span></div><div class="col-sm-4 col-xs-4 pull-right sideBar-time-align"><span class="time-meta pull-right">'+childDatamsg.time+'</span></div><div class="col-sm-10 col-xs-10 sideBar-message-align"><span class="sideBar-last-msg">'+childDatamsg.message+'</span></div><div class="col-sm-2 col-xs-2 pull-right sideBar-time-align"><span class="sideBar-msg-counter pull-right" style="">1</span></div></div></div></div>');
                                     // $(this).attr('data-id').remove();
                            });
                       }
                });
        }); 
});

我想从时间戳对数据进行排序,但结果我收到了第一条消息,但我想要根据时间戳排序的最后一条消息

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    您需要将日期/时间存储为时间戳,即自 Unix 纪元以来的时间,以毫秒为单位。

    有了实时数据库,你可以使用ServerValue.TIMESTAMP,如下:

    var messagesRef = firebase.database().ref(....);
    messages.push({
      name: .....,
      ....
      timestamp: firebase.database.ServerValue.TIMESTAMP
    });
    

    但是,由于您想使用“显示在顶部的最新(消息)”(即按时间倒序)对消息进行排序,因此您需要存储时间戳乘以 -1 的值.

    为此,您不能使用ServerValue.TIMESTAMP,因为您无法对服务器在后端填充的这个值进行任何数学运算。然后你应该使用一些标准的 JavaScript,如下所示:

    var messagesRef = firebase.database().ref(....);
    var nowTimestamp = (new Date().getTime()) * -1;
    messages.push({
      name: .....,
      ....
      timestamp: nowTimestamp
    });
    

    然后,要在您的应用中以正确的顺序显示消息,您必须使用orderByChild(),如文档中所述,此处:https://firebase.google.com/docs/database/web/lists-of-data#sort_data

    【讨论】:

    • var nowTimestamp = (new Date().getTime()) * -1;这是给 -1556283437968 所以这个值将存储在数据库中,我必须使用这个值进行排序?
    • @coder_B 没错,就是这样!
    • 嘿,非常感谢它的工作
    • 很高兴能帮到你!补充说明:如文档 (firebase.google.com/docs/reference/js/…) 中所述,“push() 生成的唯一键按当前时间排序,因此生成的项目列表将按时间顺序排序。”。但是,由于您想按时间倒序排序,因此您需要按照 anwser 中的说明进行操作。
    • 嘿,我认为它不起作用,你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2021-03-16
    • 2019-05-04
    • 2017-07-20
    • 2017-09-08
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多