firebase / geofire-js

GeoFire for JavaScript - Realtime location queries with Firebase
MIT License
1.44k stars 345 forks source link

Trying to use location variables not working for defining center. #149

Closed Moebats closed 7 years ago

Moebats commented 7 years ago

Hello, I am basically trying to pass location as variables to the query function.

So instead of hardcoding the center array like this:

    const geoQuery = geoFire.query({
      center: [37.785834, -122.4],
      radius: 10 //kilometers
    });

I am doing the following:

    let l;
    firebase.database().ref('/geofire/' + currentUser.uid).once('value').then(function(snapshot) {
      console.log(snapshot.val().l);
      l = snapshot.val().l;
      });

    const geoQuery = geoFire.query({
      center: l,
      radius: 10 //kilometers
    });

I have even tried the following:

    let l;
    let lat;
    let lng;

    firebase.database().ref('/geofire/' + currentUser.uid).once('value').then(function(snapshot) {
      console.log(snapshot.val());
      l = snapshot.val().l;
      lat = l[0];
      lng = l[1];
      });

    const geoQuery = geoFire.query({
      center: [lat, lng],
      radius: 10 //kilometers
    });

This does not seem to be working. The center property is not accepting the lat and lng I am referencing from firebase. I did a type check on the lat and lng values and they seem compatible as numbers.

Any help would be appreciated. Thanks

Moebats commented 7 years ago

I found the problem, an async action was messing up the sequence of variable updates.