gpt4 book ai didi

javascript - 是否可以只从 Firebase 检索 key 列表?

转载 作者:行者123 更新时间:2023-12-02 06:31:44 25 4
gpt4 key购买 nike

我有一个这种形式的节点:

index
$categoryId
$productId

我希望主要检索 index 的键列表,因此是一个包含索引中所有可用 $categoryId 的对象。

有没有办法在 Firebase 中检索此列表?我假设检索键列表比检索包含来自 $categoryId 的所有数据的列表更有效。

我知道另一种方法是维护所有类别的索引列表,但这需要更多编码(当类别被编辑和删除时)。

最佳答案

您可以使用 shallow keys option在 REST API 中,但首选索引。

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

不幸的是,实时 SDK 中没有解决方案。但是,我建议构建数据以避免必须使用 REST API。尝试在您的 Firebase 数据库中存储 $categoryId 的索引。我知道这是额外的代码,但它通常是一行代码。

{
"lookupCategory": {
"category_one": true,
"category_two": true,
"category_three": true,
"category_four": true,
"category_five": true
}
}

有了这个索引,您可以只进行实时监听,或者如果愿意,可以执行 .once()

var ref = new Firebase('<my-firebase-app>/lookupCategory');
ref.on('value', (snap) => console.log(data));

关于javascript - 是否可以只从 Firebase 检索 key 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34200193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com