Check if value exists in firebase DB

后端 未结 4 2004
萌比男神i
萌比男神i 2020-11-29 20:24

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.

I have the followi

相关标签:
4条回答
  • 2020-11-29 20:52

    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.

    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.

    //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();
       }
    });
    
    0 讨论(0)
  • 2020-11-29 20:58

    This is how I searched for a url value in Firebasebase database on my project:

    
    const searchFirebase = (url, idx) => 
    {
         firebase
           .app()
           .database()
           .ref("recipes")
           .orderByChild("url")
           .equalTo(url)
           .once("value", (snapshot) => {
             if (snapshot.exists()) {
               const userData = snapshot.val();
               console.log("exists!", userData);
               document.querySelector(`[data-recipe-id="${idx}"]`)
               .classList.replace('fav-heart', 'unfav-heart'); 
             } else {
               return false; 
             }
           });
    }
    
    
    0 讨论(0)
  • 2020-11-29 21:01

    This is a similar solution if you want to check if an email exists in 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
                  });
                }
            });
    
    0 讨论(0)
  • 2020-11-29 21:11

    I can't make comments yet, so I'm posting an answer:

    To add to the suggested answer, if you wanted to query whether or not any users exist, you can try to improve performance by adding .limitToFirst(1) or .limitToLast(1). This way you can limit your retrieved data to the minimum:

    //Check if any users exist
    firebase.database().ref(`users`).limitToFirst(1).once("value", snapshot => {
       if (snapshot.exists()){
          console.log("exists!");
          // TODO: Handle that users do exist
          return true;
       }
    
       // TODO: Handle that users do not exist
    });
    

    sources: https://firebase.google.com/docs/reference/js/firebase.database.Query#limitToFirst

    0 讨论(0)
提交回复
热议问题