admin管理员组文章数量:1404923
I have a node in this form:
index
$categoryId
$productId
I wish to retrieve primarily a list of the keys of index
, thus an object filled with all the available $categoryId
in index.
Is there a way to retrieve this list in Firebase? I am assuming that it is more efficient to retrieve a list of keys than a list with all the data from $categoryId
.
I understand that an alternative way is to maintain an index list of all the categories, but this requires some more coding (when categories get edited and deleted).
I have a node in this form:
index
$categoryId
$productId
I wish to retrieve primarily a list of the keys of index
, thus an object filled with all the available $categoryId
in index.
Is there a way to retrieve this list in Firebase? I am assuming that it is more efficient to retrieve a list of keys than a list with all the data from $categoryId
.
I understand that an alternative way is to maintain an index list of all the categories, but this requires some more coding (when categories get edited and deleted).
Share Improve this question edited Sep 22, 2017 at 18:01 CommunityBot 11 silver badge asked Dec 10, 2015 at 11:00 NoodlioNoodlio 337 bronze badges1 Answer
Reset to default 10You can use the shallow keys option in the REST API, but indexing is preferred.
Plnker demo.
fetch('<my-firebase-app>/category/.json?shallow=true')
.then((response) => {
return response.json()
})
.then((data) => console.log(data)) // prints only keys under category
Unfortunately, there's no solution in the realtime SDKs. But, I would remend structuring your data to avoid having to use the REST API. Try storing an index of $categoryId
in your Firebase database. I know this is extra code, but it's usually a one-liner.
{
"lookupCategory": {
"category_one": true,
"category_two": true,
"category_three": true,
"category_four": true,
"category_five": true
}
}
With this index, you can just do a realtime listen, or a .once()
if preferred.
var ref = new Firebase('<my-firebase-app>/lookupCategory');
ref.on('value', (snap) => console.log(data));
本文标签: javascriptIs it possible to only retrieve a list of keys from FirebaseStack Overflow
版权声明:本文标题:javascript - Is it possible to only retrieve a list of keys from Firebase? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744863385a2629202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论