Search code examples
node.jsgoogle-cloud-firestoregeofirestore

How to actually query data from Firestore with GeoFirestore?


I'm trying to query documents near a given point in given radius in Firestore with using GeoFirestore for my node.js project. Firstly, I have a document in the database in this format: enter image description here

My query code is like this:

// Create the GeoFirestoreQuery object
const geoQuery = geoFirestore.query({
    center: new firebase.firestore.GeoPoint(latitute, longitude),
    radius: 5,
    query: (ref) => ref.where("d.available", "==", true)
});

The thing is that, I cannot figure out how to get the document from the query. I tried the code below, but it simply is not called at all.

// Fetch the nearest couriers
const onKeyEnteredRegistiration = geoQuery.on("key_entered", function(key, document, distance) {
    console.log(key);
});

How can I return the document? What I am missing? Is my query and database structure matches? Is there any missing fields or issues? Or should I use another query instead of "key_entered"? Thanks for any help in advance.


Solution

  • I found the issue, it was about the database structure. I changed my structure as shown below and now it works perfectly. enter image description here