How to get the key from a Firebase data snapshot?

前端 未结 5 1267
南旧
南旧 2021-02-01 02:52

I\'m able to query my users array with an e-mail address and return the user\'s account info:

users.orderByChild(\'email\').equalTo(authData.user.em         


        
5条回答
  •  轮回少年
    2021-02-01 03:24

    users.orderByChild('email').equalTo(authData.user.email) is a Query (doc) that you have built by "chaining together one or more of the filter methods". What is a bit specific with your query is that it returns a dataSnapshot with only one child, since you query with equalTo(authData.user.email).

    As explained here, in this exact case, you should loop over the returned dataSnapshot with forEach():

    Attaching a value observer to a list of data will return the entire list of data as a single snapshot which you can then loop over to access individual children.

    Even when there is only a single match for the query, the snapshot is still a list; it just contains a single item. To access the item, you need to loop over the result, as follows:

    ref.once('value', function(snapshot) {
      snapshot.forEach(function(childSnapshot) {
        var childKey = childSnapshot.key;
        var childData = childSnapshot.val();
        // ...
      });
    });
    

提交回复
热议问题