Search code examples
javascriptangularfirebasegeofire

GeoFire orderByChild filtering


I seem to have stumbled on an issue with GeoFire, in that I can't seem to filter my GeoFire queries. On my platform users create posts and their location is set with geofire, and because of that, it is possible that there are many thousands of posts which I don't want to filter on the client-side..

I'd like to orderByChild('created') and limitToLast('10') so it doesn't take too long on the client-side..

What I have so far is:

  this.geoFire = new GeoFire(this.database.database.ref()
  .child('location'))
  this.geoFire.query({
    center: [this.userLocation.lat, this.userLocation.lng],
    radius: 7
  })
  .on('key_entered', (key, location, distance) => {
    this.subscription = this.database.database.ref('/posts/'+key)
    .orderByChild('created')
    this.subscription.once('value', (snapshot) => {
          this.postFeed.push(snapshot.val())
    });
  })

but this doesn't seem to work.

My database for locations looks like this:

enter image description here

and for posts:

enter image description here

What am I doing wrong? Why can't I filter orderByChild('score') or 'created'


Solution

  • A Firebase query retrieves (possibly) multiple child nodes from a certain location, based on conditions you specify. You already have the complete path to the post here: database.ref('/posts/'+key). You cannot combine already knowing what post you want to retrieve with querying for posts based on conditions.

    Your best bet here is to retrieve all posts that are within range, and then filter the top 10 most recent ones on the client.