Check if value exists in firebase DB

后端 未结 4 2019
萌比男神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 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

提交回复
热议问题