【发布时间】: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 对象:
这是 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