检查Firebase阵列中特定值的正确方法

时间:2017-01-26 12:57:05

标签: javascript firebase firebase-realtime-database

我正在尝试弄清楚如何检查Firebase数据库中是否存在特定值。

这是我目前的布局:

MYAPP
    |_______________users
    |                   |_____OshwYF72Jhd9bUw56W7d
    |                   |                   |
    |                   |                   |__username
    |                   |                   |__email            
    |                   |                   |__friends             
    |                   |                            |
    |                   |                            |__KbHy4293dYgVtT9pdoW
    |                   |                            |__PS8tgw53SnO892Jhweh
    |                   |                            |__Qufi83bdyg037D7RBif
    |                   |                            |__Gicuwy8r23ndoijdakr
    |                   |
    |                   |_____KbHy4293dYgVtT9pdoW
    |                   |_____PS8tgw53SnO892Jhweh
    |                   |_____Gicuwy8r23ndoijdakr
    |
    |__conversations

我希望能够检查特定ID是否位于users/"UserID"/friends文件夹中。

我想知道我是否需要获取friends的全部内容然后使用Javascript迭代返回的数组,或者是否有Firebase方法可以在提供ID时进行检查?

这是我目前的尝试:

function checkIfFriend(receivedFriendId){
    // receivedFriendId is the ID I want to check.
    // globaluid is the currently logged in user's ID (it sits between "users" and "friends")

    // Attempting to check for the presence of receivedFriendId
    return firebase.database().ref('/users/' + globaluid + '/friends/').child(receivedFriendId).once('value', function(snapshot) {

        if (snapshot.exists()) {
            console.log("This ID exists.");
          }else{
            console.log("This ID doesn't exist.");
          }              

    });
}

这会使console.logging“此ID不存在”。即使ID确实存在于数据库中的该位置。

我想知道是否可以通过如上所述在查询中发送ID来检查ID的存在,或者我是否需要返回friends的全部内容然后遍历返回的列表以查看是否ID存在吗?

2 个答案:

答案 0 :(得分:0)

这有效:

function checkIfFriend(receivedFriendId){

  var ref = firebase.database().ref().child('/users/'+globaluid+'/friends/');

      ref.on("child_added", function(child) {

            var IDofFriends = child.val();

                if(IDofFriends == receivedFriendId){
                    console.log("The other user's ID is in the currently signed in user's friend list, they are friends!");
                }else{
                    console.log("This user's ID is NOT in the currently signed in user's friend list, they are NOT friends! So do nothing.");
                }
        });
}

虽然即使在找到匹配的ID后仍然存在继续运行数组的问题。我不确定如何阻止它运行,我已经尝试了return;并且我尝试过设置标志,但似乎都没有效果。

如果有人能够在找到匹配后让迭代停止,请将其作为答案发布,我接受而不是我的。

答案 1 :(得分:0)

我使这段代码运行完美。

 var query = firebase.database().ref("Users").orderByKey();
      query.once("value").then(function (snapshot) {
        snapshot.forEach(function (childSnapshot) {
var key = childSnapshot.key;
         

          var childData = childSnapshot.child("phoneno").val();

          if (childData == receivedFriendId) {
            // He's a friend
          } else {
            // Do something else
          }

          // Cancel enumeration
          return true;
        });
      });