简体   繁体   English

如何在不检索父节点的情况下检查密钥是否存在? [Firebase /数据库]

[英]How to check if a key exists, without retrieving the parent node? [Firebase / Database]

I want to check if a key exists in the Realtime database. 我想检查实时数据库中是否存在密钥。 I know that I can retrieve the parent not and then use .hasChild('theKey') to check for it, but the parent node might be massive and loading all of it might be expensive. 我知道我可以不检索父项,然后使用.hasChild('theKey')来检查它,但父节点可能很大并且加载所有它可能很昂贵。

I also know that I can only ask for the key from the Realtime DB, and if it does not exist, it will throw an error. 我也知道我只能从Realtime DB中请求密钥,如果它不存在,则会抛出错误。 But this method is not so good for me either because I don't know how to make sure that the thrown error is because that the key does not exist, not, for example, a network problem or something else. 但是这个方法对我来说不是很好,因为我不知道如何确保抛出的错误是因为密钥不存在,而不是例如网络问题或其他问题。

And by the way, I am using node.js in Firebase Functions for accessing the DB. 顺便说一下,我在Firebase函数中使用node.js来访问数据库。

You can use the shallow parameter for checking if a key exists. 您可以使用shallow参数来检查密钥是否存在。 If data exists, it will return either the value (for primitives) or 'true' (for objects) and if it doesn't exist, it will return null . 如果数据存在,它将返回 (对于基元)或'true' (对于对象),如果它不存在,它将返回null

Read more: https://firebase.google.com/docs/database/rest/retrieve-data#shallow 了解详情: https//firebase.google.com/docs/database/rest/retrieve-data#shallow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM