google-cloud-firestoregoogle-cloud-functionsgeofirestore

geofirestore querydocumentsnapshot


Using geofirestore, I can query from collection users/{userId}/pros in cloud functions and get resulting documents(doc). Now I want to add a new collection users/{userId}/pros/{proId}/notifs right under each of the document users/{userId}/pros/{proId} that came from the query. So, I wrote like this;

exports.sendNotification = functions.firestore
.document("users/{user}/consumers/{consumer}")
.onCreate(async snapshot => {
try {

 query.get().then(querySnapshot => {
 querySnapshot.forEach(doc => {
    
 await doc.ref.collection('notifs').add({ . . .}); 

 }).catch ((error) =>
            console.log(error));
 

However I keep getting errors TypeError: Cannot read property 'collection' of undefined. What seems to be wrong? Geofiretore queryDocumentSnapshot doesn't seem to have collection() property. Thanks for any help.


Solution

  • There is docs property missing before forEach loop. According to the QuerySnapshot documentation the property is:

    An array of all the documents in the QuerySnapshot

    So the if you want to loop over the docs it should be:

    ...
    query.get().then(querySnapshot => {
     querySnapshot.docs.forEach(doc => {
    ...
    
    

    According to my tests this will work at least directly in node (which is used in cloud function as well)