检查firebase DB中是否存在值

时间:2022-10-22 07:20:00

Is there a method in firebase, which can check if value exist in DB? Firebase has method .exists(), but according to docs it checks only the keys.

firebase中是否有方法可以检查DB中是否存在值? Firebase有方法.exists(),但根据文档,它只检查键。

I have the following structure:

我有以下结构:

{
  "users": {
    "-KKUmYgLYREWCnWeHCvO": {
      "fName": "Peter",
      "ID": "U1EL9SSUQ",
      "username": "peter01"
    },
    "-KKUmYgLYREWCnWeHCvO": {
      "fName": "John",
      "ID": "U1EL5623",
      "username": "john.doe"
    }
  }
}

I want to check if ID with value U1EL5623exists.

我想检查ID为U1EL5623的ID是否存在。

2 个解决方案

#1


36  

The exists() method is part of the snapshot object which is returned by firebase queries. So keep in mind that you won't be able to avoid retrieving the data to verify if it exists or not.

exists()方法是firebase查询返回的快照对象的一部分。因此请记住,您将无法避免检索数据以验证数据是否存在。

ref.child("users").orderByChild("ID").equalTo("U1EL5623").once("value",snapshot => {
    if (snapshot.exists()){
      const userData = snapshot.val();
      console.log("exists!", userData);
    }
});

Observations:

In case you are in a different scenario which you have the exact ref path where the object might be, you wont need to add orderByChild and equalTo. In this case you can fetch directly the path to the object so it wont need any search processing from firebase. Also, if you know one of the properties which the object must have you can do as the snippet bellow and make it retrieve just this property and not the entire object. The result will be a much faster check.

如果您在不同的场景中拥有对象可能的确切ref路径,则无需添加orderByChild和equalTo。在这种情况下,您可以直接获取对象的路径,因此它不需要从firebase进行任何搜索处理。此外,如果您知道对象必须具有的其中一个属性,您可以将其作为下面的代码段,并使其仅检索此属性而不是整个对象。结果将是一个更快的检查。

//every user must have an email
firebase.database().ref(`users/${userId}/email`).once("value", snapshot => {
   if (snapshot.exists()){
      console.log("exists!");
      const email = snapshot.val();
    }
});

#2


1  

This is a similar solution if you want to check if an email exists in firebase

如果您想检查firebase中是否存在电子邮件,这是一个类似的解决方案

firebase.app().database().ref("shops").orderByChild("email")
   .equalTo(user.email).once("value", snapshot => {

            const userData = snapshot.val();

            // Check if it is a SHOP.
            if (userData) {
              console.log("Shop logged in!");
              this.setState({
                isAdminLoggedIn: false,
                isUserLoggedIn: false,
                isShopLoggedIn: true,
                isNoneLoggedIn: false
              });

            // Check if it is a USER.
            } else {
              console.log("User logged in");
              this.setState({
                isAdminLoggedIn: false,
                isUserLoggedIn: true,
                isShopLoggedIn: false,
                isNoneLoggedIn: false
              });
            }
        });

#1


36  

The exists() method is part of the snapshot object which is returned by firebase queries. So keep in mind that you won't be able to avoid retrieving the data to verify if it exists or not.

exists()方法是firebase查询返回的快照对象的一部分。因此请记住,您将无法避免检索数据以验证数据是否存在。

ref.child("users").orderByChild("ID").equalTo("U1EL5623").once("value",snapshot => {
    if (snapshot.exists()){
      const userData = snapshot.val();
      console.log("exists!", userData);
    }
});

Observations:

In case you are in a different scenario which you have the exact ref path where the object might be, you wont need to add orderByChild and equalTo. In this case you can fetch directly the path to the object so it wont need any search processing from firebase. Also, if you know one of the properties which the object must have you can do as the snippet bellow and make it retrieve just this property and not the entire object. The result will be a much faster check.

如果您在不同的场景中拥有对象可能的确切ref路径,则无需添加orderByChild和equalTo。在这种情况下,您可以直接获取对象的路径,因此它不需要从firebase进行任何搜索处理。此外,如果您知道对象必须具有的其中一个属性,您可以将其作为下面的代码段,并使其仅检索此属性而不是整个对象。结果将是一个更快的检查。

//every user must have an email
firebase.database().ref(`users/${userId}/email`).once("value", snapshot => {
   if (snapshot.exists()){
      console.log("exists!");
      const email = snapshot.val();
    }
});

#2


1  

This is a similar solution if you want to check if an email exists in firebase

如果您想检查firebase中是否存在电子邮件,这是一个类似的解决方案

firebase.app().database().ref("shops").orderByChild("email")
   .equalTo(user.email).once("value", snapshot => {

            const userData = snapshot.val();

            // Check if it is a SHOP.
            if (userData) {
              console.log("Shop logged in!");
              this.setState({
                isAdminLoggedIn: false,
                isUserLoggedIn: false,
                isShopLoggedIn: true,
                isNoneLoggedIn: false
              });

            // Check if it is a USER.
            } else {
              console.log("User logged in");
              this.setState({
                isAdminLoggedIn: false,
                isUserLoggedIn: true,
                isShopLoggedIn: false,
                isNoneLoggedIn: false
              });
            }
        });