Querying By Multiple Keys in Firebase

前端 未结 1 951
攒了一身酷
攒了一身酷 2020-12-05 16:51

I have a list of known keys in my Firebase database

-Ke1uhoT3gpHR_VsehIv
-Ke8qAECkZC9ygGW3dEJ
-Ke8qMU7OEfUnuXSlhhl

Rather than looping thro

相关标签:
1条回答
  • 2020-12-05 17:29

    As David's comment suggested: if the items are in some way related, you may be able to built a query to get them all.

    Otherwise this would do the trick:

    var keys = [ 
      "-Ke1uhoT3gpHR_VsehIv",
      "-Ke8qAECkZC9ygGW3dEJ",
      "-Ke8qMU7OEfUnuXSlhhl"
    ];
    var promises = keys.map(function(key) {
      return firebase.database().ref("/items/").child(key).once("value");
    });
    Promise.all(promises).then(function(snapshots) {
      snapshots.forEach(function(snapshot) {
        console.log(snapshot.key+": "+snapshot.val());
      });
    });
    

    Note that retrieving each item with a separate request is not as slow as you may think, since the requests are all sent over a single connection. For a longer explanation of that, see Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly.

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