0

Here's a picture of my database image of my database.

With just one call, I'd like to retrieve a user (his uid folder) based on what's stored in featuredUser > uid. I know I can do it with 2 calls (see below), but I'd really like a 1 call solution. Help!

var uid;
database.ref('meta/featuredUser').on('value', function(snap) { uid = snap.val().uid; };
database.ref('users/'+uid).on('value', function(snap) { desiredResult = snap.val(); };
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gabe Rogan
  • 3,343
  • 1
  • 16
  • 22

1 Answers1

1

There is no way efficient to load data from two such disparate nodes with one call. The only way would be to load the lowest level that they share, which in this case would mean that you load the entire database.

Many developer are overly pessimistic about how long it'll take to load data with multiple API calls. Since Firebase pipelines the requests over a single connection, loading items with multiple calls is quite efficient. See my answer here for a longer explanation: Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    You can't pipeline two requests when the second request is dependent upon the result of the first. So, the OP's situation ends up being 100% serial. Because of connection sharing, the same connection can be used, but that isn't technically pipelining if the two requests can't be in-flight at the same time. – jfriend00 Oct 30 '16 at 00:40
  • With my nested query it's taking .5 seconds from start to finish. Would that be considered slow for a call like this? Btw thanks jfriend for that comment – Gabe Rogan Oct 30 '16 at 00:54
  • And on a third note I'll denormalize and avoid this altogether. – Gabe Rogan Oct 30 '16 at 01:01
  • @jfriend00 you're right. I missed that the requests were dependent. – Frank van Puffelen Oct 30 '16 at 03:32
  • @GabeRogan building the initial connection can take some time and is hard to predict. After that .5 seconds for two roundtrips sounds reasonable. – Frank van Puffelen Oct 30 '16 at 03:33