【问题标题】:How I can Change the orderBy firebase database parameter in Delphi?如何在 Delphi 中更改 orderBy firebase 数据库参数?
【发布时间】:2017-10-03 15:46:15
【问题描述】:

我有这个 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 对象:

消息 89 消息 90 消息 100 消息 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"}

我怎样才能改变它?

【问题讨论】:

  • 似乎已经排序了——因为键是按字母顺序排列的字符串。
  • 它的顺序是这样的 message-100, message-101, message-99, message-98, message-97,我需要按message_id 升序排列..
  • @UweRaabe ('orderByChild', '"message_id"') ,为什么这种格式不起作用,我如何通过child 订购它?
  • @junior.programmer:你试过去掉message_id周围多余的双引号吗? QueryParams.Add('orderByChild', 'message_id')
  • @RemyLebeau 我试试,我得到了这个异常{"error":"orderBy must be defined when other query parameters are defined"}

标签: delphi firebase-realtime-database


【解决方案1】:

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

通过访问您的 Firebase/Databse/Rules 并更改为类似内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2019-04-08
    • 2021-08-24
    • 2021-12-15
    相关资源
    最近更新 更多