MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
504 stars 58 forks source link

always returns empty array and when 0 returns all #174

Closed IamNotHuman closed 4 years ago

IamNotHuman commented 4 years ago

When ever I add a value to radius the query stops bringing data.

I have a collection called zones and a field called location that has firestore geopoints.

using: geofirestore@3.4.3 react-native-firebase/firestore@6.4.0

my code:

const getZones = async (location) => { const geofirestore = new GeoFirestore(firestore()); const geocollection = geofirestore.collection('zones');

return geocollection.limit(100).near({
    center: new firestore.GeoPoint(location.latitude, location.longitude),
    radius: 100
})
    .get()
    .then(snapshot => {
        console.log(snapshot, '.................')
        let markers = [];
        snapshot.docChanges().forEach(change => {
            markers.push({id: change.doc.id, ...change.doc.data()});
        });
        return markers;
    });

}

MichaelSolati commented 4 years ago

get only executes once. You want onSnapshot to get changes to Firestore. (I think this should help)

IamNotHuman commented 4 years ago

get only executes once. You want onSnapshot to get changes to Firestore. (I think this should help)

Changed it to onSnapshot but getting the same result (when I add radius nothing happens and when I add radius 0 I get all of the markers)

const geofirestore = new GeoFirestore(firestore());
    const geocollection = geofirestore.collection('zones');

    return geocollection.limit(100).near({
        center: new firestore.GeoPoint(location.latitude, location.longitude),
        radius: 100
    }).onSnapshot((snapshot: GeoQuerySnapshot) => {
        let markers = [];
        snapshot.forEach(doc => {
            markers.push({id: doc.id, ...doc.data()});
        });
        dispatch(ACTIONS.setPostMarkers(markers));
    });
MichaelSolati commented 4 years ago

Can I get a repo where this issue is reproducible?

francisleigh commented 4 years ago

@MichaelSolati I am experiencing this also. radius: 0 returns all results whereas any other number returns none.

import { useFirestore } from "react-redux-firebase";
import { GeoFirestore } from "geofirestore";
...

const firestore = useFirestore();
const geofirestore = new GeoFirestore(firestore);
const geocollection = geofirestore.collection("shops");
const locations = [
    {
        latitude: 50.819224,
        longitude: -0.122760,
        name: "Barber Blacksheep",
    },
    {
        latitude: 50.819583,
        longitude: -0.124165,
        name: "Sharp Scissors",
    },
    {
        latitude: 50.819217,
        longitude: -0.124777,
        name: "Zar's Barbers",
    },
    {
        latitude: 50.830493,
        longitude: -0.147061,
        name: "Teddy Edwards",
    },
    {
        latitude: 40.7589,
        longitude: -73.9851,
        name: "TEST",
    },
];

useEffect(() => {
    locations.forEach((location) => {
        geocollection.add({
            name: location.name,
            coordinates: new firestore.GeoPoint(location.longitude, location.latitude),
        });
    });

    const query = geocollection.near({
        center: new firestore.GeoPoint(50.818398, -0.12713671),
        radius: 0,
    }).get().then((value) => {
        console.log(value.docs.map((d) => d.data()));
    });
}, []);

For the sake of this example i've put the query in the same useEffect as the geocollection.add but i can assure you that the collection is indeed populated before calling geocollection.near If i were to put the radius: 1000000 it yields no "shops" where 0 yields all 5

I have also tried the onSnapshot approach and get the same results

Screenshot 2020-06-07 at 16 56 32

^ Example of the data structure generated by geocollection.add