![](/img/trans.png)
[英]How to check if a key exists, without retrieving the parent node? [Firebase / Database]
[英]Check if a key exists without getting all node data
我正在使用带有节点的 Firebase 实时数据库,我发现检查密钥是否存在的唯一方法是返回所有数据
{
"salas" : {
"sala01" : {
"activa" : true,
"fechaComienzo" : "2021-10-16 07:09:00Z",
"fechaCreacion" : "2021-10-16 07:09:24Z",
"nombre" : "test",
"masInfo": {
...
}
},
"sala02" : {
"activa" : true,
"fechaComienzo" : "2021-10-16 07:09:00Z",
"fechaCreacion" : "2021-10-16 07:09:24Z",
"nombre" : "test",
"masInfo": {
...
}
}
}
}
async function checkExists(_ref, key) {
const q = query(_ref, ...[orderByKey(), equalTo(key)])
const datos = await get(q);
console.log('@info', q, datos);
return datos.exists();
}
const _ref = ref(getDatabase())
checkExists(_ref, 'sala01')
对于上面的例子,如果我想知道sala01
是否存在,它会给我带来每个节点及其后代。
我想知道是否有任何其他方法可以简单地检查sala01
是否存在而不给我带来整个节点,或者是否有一些技巧可以带来一个深度的节点,就像我在 REST API 文档中看到的那样, 浅
这种查询比需要的更复杂:
const q = query(_ref, ...[orderByKey(), equalTo(key)])
如果您已经知道某个节点的键,则无需使用查询,而是可以直接访问该键。
async function checkExists(_ref, key) {
const ref = child(_ref, key)
const datos = await get(ref);
return datos.exists();
}
这不会读取整个/_ref
节点,但会读取整个/_ref/$key
节点。 如果不读取该路径,就无法测试该路径是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.