MichaelSolati / geofirestore-js

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

How to write to firestore after geofirestore query #206

Closed k0302 closed 3 years ago

k0302 commented 3 years ago

I have a question about writing to firestore from cloud functions after geofirestore query. I queried my firestore collection users/userId/pros', and got documentsusers/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.

MichaelSolati commented 3 years ago

Duplicate of issue #192