codediodeio / geofirex

:globe_with_meridians: :round_pushpin: Geolocation Queries with Firestore & RxJS

Home Page:https://geo-test-c92e4.firebaseapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

await get(query) not working in Firebase Functions

Deathblade6 opened this issue · comments

I am using geofirex in Firebase Cloud Functions using the following code:

const functions = require('firebase-functions');
 const firebase = require('firebase-admin');
firebase.initializeApp(functions.config.firebase);
const geo = require('geofirex').init(firebase);
var { get } = require('geofirex')
const firestore = firebase.firestore();
const settings = {
  timestampsInSnapshots: true
}
firestore.settings(settings)
const hostels = firestore.collection('hostels');
exports.helloWorld = functions.https.onRequest((request, response) => {
  const position = geo.point(40, -119);
  hostels.add({ name: 'Phoenix', position });
  response.send("Hello from Firebase!");
});
exports.search = functions.https.onRequest((request,response) => {
  // const center = geo.point(request.body.location[0],request.body.location[1]);
  const center = geo.point(40,-119);
  const radius = 1;
  const field  = 'position';
  getRooms(center,radius,field).then((res) => {
      console.log(res);
  }).catch((err) => {
      console.log("err"+err);
  });
  response.send("Atleast this much");
})
async function getRooms(center,radius,field) {
    const query = geo.query(hostels).within(center,radius,field);
    const snap = await get(query);
    console.log(query);
}

When executing, the code to save the data into firestore works perfectly and creates the hash and all, but the value of query when searching is returned as

Observable {
  _isScalar: false,
  source: 
   Observable {
     _isScalar: false,
     source: Observable { _isScalar: false, source: [Object], operator: [Object] },
     operator: [Function: shareReplayOperation] },
  operator: FinallyOperator { callback: [Function] } }

res has value undefined.

Does anyone know how to fix this?

Geofirex internally ises RxJs observables. geo.query returns an observable, get converts it to a promise, your snap is actually an array of matched hotels ordered by distance.