MichaelSolati / geofirestore-js

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

ERROR: Array is not a function (evaluating 'e.docChanges()') #24

Closed ektasahu027 closed 6 years ago

ektasahu027 commented 6 years ago

screenshot_2018-08-07-13-07-15

Here is my code:

 const db = firebase.firestore();
    const userCollection = db.collection('interviews');
    const geoobj = new GeoFirestore(userCollection)
    let query  = geoobj.query({
      center: coordinates,
      radius: 100,
    //  query: (ref) => ref.where('d.count', '==', 1)

    });
    query.on("ready", () => this.setState({ loading: false }));
    query.on("key_entered", (key, coords, distance) => {
      console.log("asdada",key);
    });
MvRemmerden commented 6 years ago

Probably the same reason as in #19, try to update firebase.

ektasahu027 commented 6 years ago

@MvRemmerden i am using react-native-firebase package so i have to update it...

MichaelSolati commented 6 years ago

This package isn't designed for react-native-firebase, it's designed for the pure JavaScript firebase library. Unfortunately it will not work. (I am open to PRs for support though)

ektasahu027 commented 6 years ago

@MichaelSolati thanks when i use firebase its work.

kenzdozz commented 6 years ago

@MichaelSolati Will the package work for firebase-admin sdk???

MichaelSolati commented 6 years ago

@kenzdozz it should with versions 5.x.x.

AndrewAi commented 6 years ago

Hi, @MichaelSolati Just to let you know if you don't already. I am using react-native-firebase and geofirestore(2.2.1) seems to be working fine with it, I can read and write. Thank you very much for this library it's very helpful and easy to use! keep it up, please ! :)

graig12 commented 6 years ago

Hi Michael,

Great job on the library. I am using firestore "firebase": "^4.13.1" along with geofirestore "^2.2.2". I am trying to accomplish the following query using array-contains. I have the following array example below.

array=['cat','dog'];

now i want to loop via firestore and do something like a .where('d.name','array-contains',array). See my query below for reference.

const collectionRef = firebase.firestore().collection('Consents') const geoFirestore = new GeoFirestore(collectionRef);

          var arr =['cat','dog']
          const geoQuery = geoFirestore.query({
          center: new firebase.firestore.GeoPoint(lat, long),
          radius: val,
          query: (ref) => ref.where('d.name', 'array-contains',arr)
   });

how can i achieve this using geofirestore, firebase and firestore.

graig12 commented 6 years ago

should i update my firebase to react native firebase to be able to use the array contains method along with geofirestore?

joscmw95 commented 6 years ago

@graig12 In Firebase docs, they usually do array-contains using a string parameter. Your code is using an array, I'm not sure if that's the problem.

Try: query: (ref) => ref.where('d.name', 'array-contains',arr.join(' '))

edit: However, this checks that the array at d.name contains an element with 'cat dog' What you seem to want to achieve is to do a 'OR' operator on the array of names against the DB, which is unfortunately not achievable with a single query in Firebase.

graig12 commented 6 years ago

that is correct @joscmw95 i want to achieve an OR. Since this is not possible can how would u suggest i accomplish this ?

joscmw95 commented 6 years ago

As written at the bottom of the docs:

Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

You'll have to query for every element in the array and merge the results:

var arr =['cat','dog']
var results = []
arr.forEach(el => {
    const geoQuery = geoFirestore.query({
      center: new firebase.firestore.GeoPoint(lat, long),
      radius: val,
      query: (ref) => ref.where('d.name', '==', el)
    })
    geoQuery.on('key_entered', function(key, document, distance) {
      results.append(document)
    });
})

This is assuming that you want the results in random order and it is probably not performant because geo query will need to run n times. I would suggest you to just get data from a single geo query and filter the data client side.

graig12 commented 6 years ago

HI @joscmw95 thanks for the input i will try and share the results.

graig12 commented 6 years ago

Hi @joscmw95 are you able to share any insight here on this post of stack over flow."https://stackoverflow.com/questions/53094022/firestore-multiple-where-query?noredirect=1#comment93100280_53094022" I am attempting to do a filter on a geofirestore/firestore database that conatians a collection of books. I am trying to filter on booktype and book age however i am getting the error "uncaught error in onsnapshot firebaseError: cursor position is outside the range of the original query " the code that i am using is below.

const collectionRef = firebase.firestore().collection('Books')

       collectionRef.where('d.details.BookType',"==",BookType)
       collectionRef = collectionRef.where('d.details.bookage',"<=",age)
       collectionRef = collectionRef.orderBy('d.details.bookage')

         const geoFirestore = new GeoFirestore(collectionRef)

          const geoQuery = geoFirestore.query({
          center: new firebase.firestore.GeoPoint(lat, long),
          radius: val,

          });

        geoQuery.on("key_entered",function(key, coords, distance) {   

storeCoordinate(key,coords.coordinates._lat,coords.coordinates._long,newdata) });

joscmw95 commented 6 years ago

Internally geoFirestore gets its results by using startAt and endAt. The way you declare your collectionRef clashes with how it works hence the cursor error.

Detailed explanation in stackoverflow