Is it possible to query multiple keys in firebase (node.js) at once? -
currently doing foreach loop populate array based on user's favorites:
usersref.child(formdata.openid + '/favorites').once('value', function(snapshot){ var favlist = []; snapshot.foreach(function(fav){ fav = fav.key(); ref.child(fav).once('value',function(snapshot){ favlist.push(snapshot.val()); }); }); response.writehead(200, {'content-type': 'application/javascript'}); response.write(favlist); response.end(); });
usersref
database user, ref
database items. formdata.openid
user's unique id.
since 2 lists disjunct, you'll need separate read each item. ensure reads done before sending response client, can use promises:
usersref.child(formdata.openid + '/favorites').once('value', function(snapshot){ var promises = []; snapshot.foreach(function(fav){ promises.push(ref.child(fav.key()).once('value')); }); promise.all(promises).then(function(snapshots) { var favlist = snapshots.map(function(snapshot) { return snapshot.val(); }); response.writehead(200, {'content-type': 'application/javascript'}); response.write(json.stringify(favlist)); response.end(); }).catch(function(error) { response.status(500).send(error); }); });
Comments
Post a Comment