MichaelSolati / geofirestore-js

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

Filtering in GeoStore - Show more fields in the results #64

Closed rajeshgadepalli closed 5 years ago

rajeshgadepalli commented 5 years ago

Hi Team,

I am using GeoFirestore. Apart from filtering the data by distance, I also need to filter the results by a field.

This is the init code

const firebase = require('firebase');
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const GeoFirestore = require("geofirestore").GeoFirestore;

//Initialize Firebase
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

And this is the query.

    const userQRObject = db.collection('UserQRObject');
    var geoFirestore = new GeoFirestore(userQRObject);

    const geoQuery = geoFirestore.query({
        center: new firebase.firestore.GeoPoint(17.500362, 78.587374),
        radius: 2.0,
        query: (ref) => ref.where('profession', '==', 'cricketer')
    });
    const results = [];
    geoQuery.on('key_entered', (key, document) => results.push(document));
    geoQuery.on('ready', () => response.status(200).json(results));

The structure of my collection is

image

I would like to filter on the profession field. And i am able to do it.

The document object in the results array just contains the coordinates only. I would also like to show the name, phone and profession in the results array.

I am not so much in favor of keeping these fields in the d map object. Is there anyway that I can capture the complete document in the result ?

MichaelSolati commented 5 years ago

It looks as if you did not use geofirestore to add objects to the database, as the entire object must be stored in the d. field.

If this is the case then this is an issue with how you are putting data into geofirestore, not the library itself (it's behaving as expected).

So did you add the documents with geofirestore?

rajeshgadepalli commented 5 years ago

@MichaelSolati since the requirement to add the Geo Coordinates and search by them was a recent one, I am using Geofirestore to set the coordinates only. I did not want to disturb the existing document structure of UserQRObject for the sake of Geofirestore. Hence this scenario.

Code below on how I am setting the coordinates and how I am setting the rest of the fields

    var requestBody=request.body;
    if(requestBody===null){
        response.status(400).json({error:'Invalid Request Data'});
    }

    var userId = requestBody.userId;
    var qrtype = requestBody.qrType;
    var fields = requestBody.fields;
    const userQRObject = db.collection('UserQRObject');
    var geoFirestore = new GeoFirestore(userQRObject);
    geoFirestore.set(userId, { coordinates: new admin.firestore.GeoPoint(17.501303, 78.588136)}).then(() => {
      }).then(() => {
        db.collection('UserQRObject').doc(userId).update(fields)
        .then( doc => {
                response.status(200).json('User QR Object Successfully Saved');
        }).catch(function(error) {
            console.error('Error saving document: ', error);
            response.status(400).json('Error saving document: ' + error);
        });
    }, (error) => {
        console.log('Error: ' + error);
        response.status(400).json({data:error});
    });
MichaelSolati commented 5 years ago

Geofirestore is intended to wrap the document, so you shouldn't be trying to store details on the same level as d.. That's how geofirestore works, it was originally based off of geofire, and that's effectively how that worked. I'd suggest reworking your data to fit the structure geofirestore uses.

(This may change in a later version, however this can not change now as it could/would break functionality for other users and will require a gradual change)