MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.

Home Page:https://geofirestore.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to write to firestore after geofirestore query

k0302 opened this issue · comments

commented

I have a question about writing to firestore from cloud functions after geofirestore query.
I queried my firestore collection users/userId/pros', and got documents users/userId/pros/proId1' and `users/userId/pros/proId2' from the geofirestore query.
Now, I want to add 'notif' collection to these documents - proId1, proId2. For that, I wrote code in cloud functions like this;

const firestore = admin.firestore(); 
const geofirestore = new GeoFirestore(firestore);             
exports.sendNotification =                                           
functions.firestore.document("users/{userId}/clients/{client}")    
.onCreate(async snapshot => {  
const clientfield = snapshot.data().field; 
const clientgeopoint =  snapshot.data().g.geopoint; 
const geocollection = geofirestore.collectionGroup('pros');   
const query = geocollection.near({center: clientgeopoint, radius: 
10}).where('field', '==',       clientfield); 
 await query.get().then( querySnapshot => { 
querySnapshot.forEach(async doc    => { await 
doc.ref.collection('notifs').add({ 'field': clientfield, ... 
}); }); }).catch ((error) => console.log(error) ); })

After executing this code, I get error on cloud functions console saying 'TypeError: Cannot read property 'collection' of undefined'. Please give me some tip on how to fix this code to make this work. Thanks.

Duplicate of issue #192