Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all documents within a radius #43

Closed
24dev opened this issue Oct 9, 2018 · 6 comments
Closed

Get all documents within a radius #43

24dev opened this issue Oct 9, 2018 · 6 comments
Labels
question Further information is requested

Comments

@24dev
Copy link

24dev commented Oct 9, 2018

Trying to retrieve all the documents that fall within the radius of a certain point.

Query:

const geoQuery = geoFirestore.query({
    center: new firebase.firestore.GeoPoint(51.5074, 0.1278),
    radius: 5,
  });

  geoQuery
    .query()
    .get()
    .then(querySnapshot => {
      querySnapshot.forEach(doc => {
        console.log(doc.data());
      });
    })

Firestore (documents set by geofirestore.set():
Document 1 (same as search point):

coordinates: [51.5074, 0.1278]

Document 2:

coordinates: [41.1537, -8.612549]

No matter what I try, it is always returning both results, even though they are 100's of kms away from eachother. Can someone please recommend what I can do to make this work?

Much appreciated! :)

@MichaelSolati
Copy link
Owner

That seems odd, and there are test in place for just this sort of issue. Could you provide a working (broken) example?

@24dev
Copy link
Author

24dev commented Oct 9, 2018

@MichaelSolati Haven't got anything live, but what extra info would you like to see?

This is my collection:

screen shot 2018-10-09 at 17 59 51

After making the request as shown above, I get this response:

screen shot 2018-10-09 at 18 02 58

+++++

This is my firebase config, if useful:

firebase.initializeApp(firebaseConfig);
firebase.firestore().settings({ timestampsInSnapshots: true });
const db = firebase.firestore();
const geostoreRef = db.collection('exploreMap');

// Create a GeoFirestore index
const geoFirestore = new GeoFirestore(geostoreRef);

@MichaelSolati
Copy link
Owner

In all sincerity a broken app would be helpful. I'm doing tests and fixes right now for other issues and have not seen this issue. So an example would be preferred. It can be simple, it just need to be broken.

@MichaelSolati MichaelSolati added the question Further information is requested label Oct 9, 2018
@MichaelSolati
Copy link
Owner

Ok, so I just took a better look at your code, and I feel really silly not to have realized this before, but I do not think you are using the library correctly. In fact I'm surprised you're getting anything at all the way you are using it.

The below code creates a GeoFirestoreQuery:

const geoQuery = geoFirestore.query({
  center: new firebase.firestore.GeoPoint(51.5074, 0.1278),
  radius: 5,
});

If you wanted to make a geoquery you'd use the on listener for the key_entered event which will return documents in your query, see here.

However you're calling the query function, which returns a Firestore Query, or CollectionReference (depending on if you passed in a query function when you created or updated the query criteria).

Calling get on this query BYPASSES all of the GeoFirestore magic goodness, and would not provide you with what you want or expect... Instead you'd want to do something like this.

// Store all results from geoqueries here
let results = [];

// Create geoquery
const geoQuery = geoFirestore.query({
  center: new firebase.firestore.GeoPoint(51.5074, 0.1278),
  radius: 5,
});

// Remove documents when they fall out of the query
geoQuery.on('key_exited', ($key) => {
  const index = results.findIndex((place) => place.$key === $key);
  if (index >= 0) results.splice(index, 1);
});

// As documents come in, add the $key/id to them and push them into our results
geoQuery.on('key_entered', ($key, result) => {
  result.$key = $key;
  results.push(result);
});

I'll be closing this for now, as I think I have your issue/question addressed. However please let me know if I am mistaken and we can reopen this and go from there.

@24dev
Copy link
Author

24dev commented Oct 10, 2018

Brilliant, that fixed it. Sorry for my misunderstanding! The key_entered/exited is what confused me. Thanks for the great library!

@stevenyix
Copy link

i also had the same issue as @AlcuinGuest. finding this issue and explanation solved my dilemma as well. thank you for providing the info and great tools.

food for thought: when i read the docs i interpreted 'key_entered' and 'key_exited' as some type of keyboard events for a browser client-side scenario. i'm also fairly new to node.js. nonetheless, i was about to give up until i found this. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants