通过Firebase中的多个键查询 [英] Querying By Multiple Keys in Firebase
问题描述
我的Firebase数据库中已知密钥的列表
I have a list of known keys in my Firebase database
-Ke1uhoT3gpHR_VsehIv
-Ke8qAECkZC9ygGW3dEJ
-Ke8qMU7OEfUnuXSlhhl
如何获取各个对象的快照我在一个统一的请求中查询每个这些密钥? Firebase是否提供这种功能?
Rather than looping through each of these keys to fetch a snapshot of their respective object, how can I query for each of these keys in one single, unified request? Does Firebase provide this?
我发现了 Promise.all()
打算我发誓),但我不知道如何使用获取firebase数据的标准方式来实现它,如这样
I've discovered the Promise.all()
function which looks promising (no pun intended I swear) but I'm not sure how to implement it using the standard way of fetching firebase data like so
var userId = firebase.auth().currentUser.uid;
return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = snapshot.val().username;
});
感谢您的帮助!
Thanks for any help!
推荐答案
否则这样做会有诀窍:
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.
这篇关于通过Firebase中的多个键查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!