从Firebase取回 [英] Retrieve from Firebase
问题描述
我创建了一个Firebase数据库,并使用Push()方法将数据保存到数据库中。这工作正常。但是,我面临的这个问题是当我检索数据。由于Push()通过创建一个临时密钥来工作,我不知道如何进入该树的子进程,使用orderByValue()进行排序。我的JSON如下所示:
{
videos:{
videoId1:{
-KhxlA5-yIHYq-7PQC84:{\url \:\testUrl1 \,\title \:\test1 \,\x \: - 3.9357306957244875,\y \:6.528204441070557,\z \:2.5558924674987795,\time \:7982.197021484375},
-KhxlAEpJp30xF5n6kc5:{ \ url\:\ testUrl2\,\ title\:\ test2\,\ x\:4.0330400466918949,\ y\: 6.52820348739624,\z\:2.3994064331054689,\time \:2637.8599853515625},
-KhxlAPyGEI7AxFssG_:{\url \:\testUrl3 \ \ title\:\ test3\,\ x\: - 3.446043014526367,\ y\:6.52820348739624,\ z\: - 3.1854910850524904,\\ \\time \:5350.770263671875}
}
}
}
我的要求是我需要基于th对其进行排序e 时间
键,同时检索数据。
我可以到达 videoId1
树,但不知道如何超越。 (使用 reference.Child(videos)。Child(videoUrl)
)
谢谢。
您正在为 videoId1
下的每个子项存储一个字符串值。没有办法有效地查询该字符串内。
如果您想要在这些子项下进行查询,请确保将数据存储为JSON,以便总计树变成:
{
videos:{
videoId1:{
-KhxlA5-yIHYq-7PQC84:{
url:testUrl1,
title:test1,
x:-3.9357306957244875,
y:6.528204441070557,
z 2.5558924674987795,
time:7982.197021484375
}
,
-KhxlAEpJp30xF5n6kc5:{
url:testUrl2,
title :test2,
x:4.0330400466918949,
y:6.52820348739624,
z:2.3994064331054689,
time:2637.8599853515625
}
,
-KhxlAPyaGEI7AxFssG_:{
url:testUrl3,
title:test3,
x:-3.446043014526367 ,
y:6.52 820348739624,
z:-3.1854910850524904,
time:5350.770263671875
}
}
}
}
$ c
$ b $ p
$ b pre
code> ref.child('videos / videoId1')。orderByChild('time')...
I have created a Firebase DB and I'm using the Push() method to save data into the DB. This works fine. But this issue I'm facing is when I retrieve the data. As Push() works by creating a temporary key, I don't know how to step into the child of that tree to perform sorting using orderByValue(). My JSON looks like this:
{
"videos" : {
"videoId1" : {
"-KhxlA5-yIHYq-7PQC84" : "{\"url\":\"testUrl1\",\"title\":\"test1\",\"x\":-3.9357306957244875,\"y\":6.528204441070557,\"z\":2.5558924674987795,\"time\":7982.197021484375}",
"-KhxlAEpJp30xF5n6kc5" : "{\"url\":\"testUrl2\",\"title\":\"test2\",\"x\":4.0330400466918949,\"y\":6.52820348739624,\"z\":2.3994064331054689,\"time\":2637.8599853515625}",
"-KhxlAPyaGEI7AxFssG_" : "{\"url\":\"testUrl3\",\"title\":\"test3\",\"x\":-3.446043014526367,\"y\":6.52820348739624,\"z\":-3.1854910850524904,\"time\":5350.770263671875}"
}
}
}
My requirement is that I need to sort it based on the time
key while retrieving the data.
I can reach until the videoId1
tree but don't know how to get past that. (Using reference.Child ("videos").Child (videoUrl)
)
Thanks.
You're storing a string value for each child under videoId1
. There is no way to usefully query inside that string.
If you want to query under those children, make sure to store the data as JSON, so that the total tree becomes:
{
"videos": {
"videoId1": {
"-KhxlA5-yIHYq-7PQC84": {
"url": "testUrl1",
"title": "test1",
"x": -3.9357306957244875,
"y": 6.528204441070557,
"z": 2.5558924674987795,
"time": 7982.197021484375
}
",
"-KhxlAEpJp30xF5n6kc5": {
"url": "testUrl2",
"title": "test2",
"x": 4.0330400466918949,
"y": 6.52820348739624,
"z": 2.3994064331054689,
"time": 2637.8599853515625
}
",
"-KhxlAPyaGEI7AxFssG_": {
"url": "testUrl3",
"title": "test3",
"x": -3.446043014526367,
"y": 6.52820348739624,
"z": -3.1854910850524904,
"time": 5350.770263671875
}
}
}
}
With that you can query as Kirill suggested:
ref.child('videos/videoId1').orderByChild('time')...
这篇关于从Firebase取回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!