如何在Delphi中更改orderBy firebase数据库参数?

时间:2017-10-03 15:46:16

标签: delphi firebase-realtime-database

我有这个JSON对象

{
"message-100": {
    "chat_id": 69,
    "created_at": {
        "date": "2017-10-03 13:15:38.000000",
        "timezone": "UTC",
        "timezone_type": 3
    },
    "file": "",
    "message": "How r u?",
    "message_id": 100,
    "sender": 7
},
"message-101": {
    "chat_id": 69,
    "created_at": {
        "date": "2017-10-03 13:15:59.000000",
        "timezone": "UTC",
        "timezone_type": 3
    },
    "file": "",
    "message": "fine ...",
    "message_id": 101,
    "sender": 28
},
"message-89": {
    "chat_id": 69,
    "created_at": {
        "date": "2017-10-03 11:23:19.000000",
        "timezone": "UTC",
        "timezone_type": 3
    },
    "file": "",
    "message": "How r u?",
    "message_id": 89,
    "sender": 28
},
"message-90": {
    "chat_id": 69,
    "created_at": {
        "date": "2017-10-03 11:23:52.000000",
        "timezone": "UTC",
        "timezone_type": 3
    },
    "file": "",
    "message": "test",
    "message_id": 90,
    "sender": 7
}}

如何使用orderBy订购?我的意思是检索按升序排序的JSON对象:

message-89
message-90 
message-100
message-101

以下是Delphi中的firebase数据库代码:

procedure TFirebaseChatFacade.StartListenChat;
begin
  Run := True;
  TTask.Run(
    procedure
    var
      FFC: IFirebaseDatabase;
      Response: IFirebaseResponse;
      I: Integer;
      QueryParams: TDictionary<string, string>;
    begin
      FFC := TFirebaseDatabase.Create;
      FFC.SetBaseURI(FBaseURI);
      FFC.SetToken(FToken);
      QueryParams := TDictionary<string, string>.Create;
      try
        QueryParams.Add('orderBy', '"$key"'); // How I can change this parameter ???
        QueryParams.Add('limitToLast', '20');
        //////////////////////////////////////////

      finally
        QueryParams.Free;
      end;
    end);
end;

我尝试更改orderBy参数,但每次收到异常时都会告诉我错误:

{"error":"Index not defined, add \".indexOn\": \"key\", for path \"/chats/chat-69\", to the rules"}

{"error":"Index not defined, add \".indexOn\": \".value\", for path \"/chats/chat-69\", to the rules"}

{"error":"orderBy must be a valid JSON encoded path"}

{"error":"Index not defined, add \".indexOn\": \"message_id\", for path \"/chats/chat-69\", to the rules"}

我如何改变它?

1 个答案:

答案 0 :(得分:1)

您可以解决此错误:{"error":"Index not defined, add \".indexOn\": \"key\", for path \"/chats/chat-69\", to the rules"}

访问您的Firebase /数据库/规则并更改为:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "yourdata": {
      ".indexOn": ["message_id"]
    } 
  }
}