简体   繁体   English

检查 firebase 实时数据库中是否存在值?

[英]Check for existence of value in firebase realtime database?

I'm using V9 (modular) version of Firebase realtime database.我正在使用 Firebase 实时数据库的 V9(模块化)版本。 I have a list of URLs with auto-generated keys using push() .我有一个 URL 列表,其中包含使用push()自动生成的密钥。 This is the data structure.这就是数据结构。

在此处输入图像描述

What I want is to check for the existence of one of these url's in the list.我想要的是检查列表中是否存在这些 url 之一。 Here's what I've attempted.这是我尝试过的。

    const testURL = "https://i.redd.it/bm6psl9sgsj81.png"; //First one in the list
    const db = getDatabase();
    const dbRef = ref(db, "burnedURLs/");
    const q = query(dbRef, equalTo(testURL));
    const snapshot = await get(q);
    const results = snapshot.toJSON();
    console.log(results); //Returns NULL

How does one check the existence of a value, or just retrieve a value when you don't know the key?如何检查一个值是否存在,或者在您不知道密钥时只检索一个值? The documentation for V9 isn't very good and most examples I've found are for V8. V9 的文档不是很好,我找到的大多数示例都是针对 V8 的。 Thanks for any help.谢谢你的帮助。

Edit: Using exists() returns false when it does exist (It's the first child in the tree).编辑:使用exists()在它确实存在时返回 false(它是树中的第一个孩子)。 I think the issue is that I'm not forming the query correctly and that's what I need help with.我认为问题在于我没有正确形成查询,而这正是我需要帮助的地方。

The get(<query>) returns a DataSnapshot that still has exists() which returns true if this DataSnapshot contains any data.: get(<query>)返回一个仍然有exists()DataSnapshot ,如果此DataSnapshot包含任何数据,则返回true 。:

const q = query(dbRef, orderByValue(), equalTo(testURL));

const snapshot = await get(q);

if (snapshot.exists()) {
  const results = snapshot.val();
  console.log('Results', results)
} else {
  console.log('Data does not exist')
}

From the documentation,从文档中,

To filter data, you can combine any of the limit or range methods with an order-by method when constructing a query.要过滤数据,您可以在构造查询时将任何 limit 或 range 方法与 order-by 方法结合使用。

So adding orderByValue() in query should solve the issue.所以在查询中添加orderByValue()应该可以解决问题。

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

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