MichaelSolati / geofirestore-js

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

How to catch error of `GeoFirestore.Query()`? #60

Closed thomasdelgado closed 5 years ago

thomasdelgado commented 5 years ago

Hi,

I've been learning how to use GeoFirestore and could make work thanks to issues and some examples that I've got from here. Thanks!

However, I wonder how can I catch errors emitted from query() methods? Couldn't find how to grab those, and as you can see, errors such "you need to create index" are important. Also, not being able to catch those errors are making the application going idle forever.

Here's the method that I'm using (adapted from Michael, which is working) and useful error that I want to grab.

screenshot 2018-12-19 14 00 20

Thanks!

Wilker-uwu commented 5 years ago

when you query with GeoFirestore you will be working with Promises, so to catch an Error with it you should be able to either handle promise rejections with the second parameter in .then(callbackSuccess, callbackError) by calling it in the return value, or you should just put on a try/catch inside an async function

tell me if i gave any wrong information, i hope i helped
thomasdelgado commented 5 years ago

Hey @Wilker-a! Thanks for answering!

Yes, that was the first thing that I've tried, put that code in a try/catch block. However query() didn't throw anything. Second thing I tried was the .then(callbackSuccess, callbackError), but neither query() or query.on() have a callbackError. :/

MichaelSolati commented 5 years ago

I may not have time to work on this, but I'm open to anyone who wants to create a PR for this. But maybe I'll be able to look at it during the holiday break.

kkusanagi commented 5 years ago

hi, i'm new to this and I'm curious about the data structure on query. after I add the query as below it come out error.

this.geoFirestore.query({
        center: new firebase.firestore.GeoPoint(coorobj.latitude, coorobj.longitude),
        radius: 10,
        query: (ref)=> ref.where('d.name', '==', '123')
      });

Error:

SnapshotError: Firestore: Operation was rejected because the system is not in a state required for the operation`s execution. (firestore/failed-precondition).

Is this indexing error?

My data structure is as below:

{ d:{ coordinates: [6.09593° N, 100.37299999999999° E] createdat: 20 December 2018 at 23:26:37 UTC+8 last_update: 20 December 2018 at 23:26:37 UTC+8 lastupdateby: "3qL1ZfwrZpZsNe50fsR7LNUaVXO2" name: "test" }, g:"w1p6sj9rk7" l: [6.09593° N, 100.37299999999999° E] }

I tried read back the document in indexing. Seems like it need to be indexed in firestore before doing sub-query.

Is my query correct? and the data structure correct?

MichaelSolati commented 5 years ago

@kkusanagi your data structure is correct, and yes an index is needed to make a query, but that is an issue I have not seen before. Try an index first and let us know if that works. If not I may ask you to create a broken example of your issue

kkusanagi commented 5 years ago

@MichaelSolati it did works after I add the index in firestore. Thanks. Added index for [d.name] and [g] fields.

By the way, I can't use try-catch on GeoFirestore.Query(). Can't catch the index error. Below code also can't run when error and unable to catch error.

 const onReadyRegistration = this.geoQuery.on('ready', () => {
        console.log('GeoFirestoreQuery has loaded and fired all other events for initial data');
      });
MichaelSolati commented 5 years ago

@kkusanagi && @thomasdelgado what I'm going to have to do is check to see if I can catch an internal Firestore error. I'll check over the weekend. If I can catch it I'll pass it to the user so it doesn't fail silently and have a patch out over the weekend. Otherwise, I may not be able to do anything about it (we will see)

MichaelSolati commented 5 years ago

Okay, so this error gets thrown in an interesting place, but I think I have a solution in place that will match how Geofirestore works. I think I'll create a new event listener for errors and possibly push that up in the next day or so.