2021年1月18日星期一

add a subcollection under each document that resulted from geofirestore query

I queried 'users/userid/pros' firestore collection using cloud functions geofirestore, and I get a few specific documents('users/userid/pros/proid') from the query. Now, I want to add a new 'notifs' collection subsequently under each of these specific documents I get from the query. Here is my code to implement that functionality.

const functions = require('firebase-functions');  const admin = require('firebase-admin');  var GeoFirestore = require('geofirestore').GeoFirestore;    admin.initializeApp();    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)          );   })  

But this code gives me an error 'TypeError: Cannot read property 'collection' of undefined' on cloud functions console. How can I fix my code in order to add 'notifs' collection right under each of these specific documents like in the picture? Thanks.

https://stackoverflow.com/questions/65697903/add-a-subcollection-under-each-document-that-resulted-from-geofirestore-query January 13, 2021 at 04:10PM

没有评论:

发表评论