【问题标题】:Query for sorting a date that is inside another object in firebase查询对firebase中另一个对象内的日期进行排序
【发布时间】:2018-11-18 01:26:27
【问题描述】:

我正在使用 angularjs 开发一个聊天,其中有一个聊天列表视图和一个聊天视图。

我正在尝试做的是对我的聊天列表进行排序,以使用 Firebase 和 JS 在顶部显示最近的聊天,我进行了研究,但我发现的所有问题都直接在小时候有日期,但在我的DB 我的日期在“lastMessage”键中。我的日期格式如下。

date: 1528417493034

任何帮助将不胜感激

我的结构如下:

"Users": {
"nCsXbTl8CcXvoviuL5Tt7kiV6Bn1" : {
      "contacts" : {
        "VN9AFnn4uXgDfoir8DWHnw54zrJ3" : {
          "email" : "test@test.com",
          "lastMessage" : {
            "date" : 1528417493034,
            "senderId" : "VN9AFnn4uXgDfoir8DWHnw54zrJ3",
            "status" : "success",
            "text" : "Yes?",
            "type" : "text",
            "uid" : "1528417493034VN9AFnn4uXgDfoir8DWHnw54zrJ3"
          },
          "name" : "test"
        },
        "cHuR26YaSgbO7ahSVLg1XG5HYer2" : {
          "email" : "aaa@aaa.com",
          "lastMessage" : {
            "date" : 1528417068249,
            "senderId" : "cHuR26YaSgbO7ahSVLg1XG5HYer2",
            "status" : "success",
            "text" : "Trigeeeers?",
            "type" : "text",
            "uid" : "1528417068249cHuR26YaSgbO7ahSVLg1XG5HYer2"
          },
          "name" : "aaa"
        }
      }
}

我的聊天列表视图如下:

<div layout="row" class="main-chat-container" layout-wrap>
<div class="list-container" flex="30">
    <div class="chat-header">
        <p>Lista de Chats</p>
    </div>
    <div class="chats-list">
        <div class="chats-header">
            <p>Usuarios</p>
        </div>
        <div class="chats" ng-repeat="chat in chats">
            <div class="email-container" ng-click="setUserData(chat)" ng-class="styleData.email == chat.email ? 'new-background' : ''">
                <a>{{chat.email}}</a> <p ng-show="counter > 1">{{testData.number}}</p>
            </div>
        </div>
    </div>
</div>
<div class-"chat-container" flex="70">
    <ui-view></ui-view>
</div>

控制器如下

app.controller('chatController', function (currentAuth, $scope, $firebaseArray, $firebaseObject) {

$scope.chats = [];

$scope.getContactsList = function(){
    var listRef = firebase.database().ref('Users').child(currentAuth.uid).child('contacts');
    var test = firebase.database().ref('Users').child(currentAuth.uid).child('contacts');
    var listArray = $firebaseArray(listRef);
    var testArray = $firebaseArray(test);
    $scope.chats = listArray;

    console.log("TEST ARRAY IS ");
    console.log(testArray);



}

$scope.getContactsList();
});

【问题讨论】:

  • 您的代码和示例数据结构不匹配。例如。样本中没有contacts。不要显示数据模型,而是显示您尝试查询的实际 JSON 片段(作为文本,请不要截图)。您可以通过单击Firebase Database console 中的“导出 JSON”链接来获取此信息。
  • 我改了,你知道怎么查询吗?提前致谢。 @FrankvanPuffelen

标签: javascript angularjs firebase firebase-realtime-database


【解决方案1】:

要让用户根据上一条消息的时间戳联系,您可以执行以下操作:

var userRef = firebase.database().ref('Users').child(currentAuth.uid)
var contactsQuery = userRef.child('contacts').orderByChild("lastMessage/date");

请注意,这将按时间顺序为您提供联系人,因此您必须在客户端中反转结果。

或者,有些人在他们的数据库中的一个单独的属性中保存一个反转的值,例如

    "VN9AFnn4uXgDfoir8DWHnw54zrJ3" : {
      "email" : "test@test.com",
      "lastMessage" : {
        "date" : 1528417493034,
        "invertedDate" : -1528417493034,
        "senderId" : "VN9AFnn4uXgDfoir8DWHnw54zrJ3",

由于此 invertedDate 值是倒置的,因此对该属性的排序将按时间倒序为您提供联系人,因此您不必在客户端中颠倒它们。

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多