I am trying the following function to count the total of documents in my collection: https://stackoverflow.com/a/49407570/8312532 but I see that the firebase functions have been updated: https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore
Therefore I am trying this:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.documentWriteListener =
functions.firestore.document('domiciliarios/{uid}')
.onWrite((change, context) => {
if (!change.before) {
// New document Created : add one to count
db.doc(docRef).get().then(snap => {
db.doc(docRef).update({numberOfDocs: snap.numberOfDocs + 1});
return;
});
} else if (change.before && change.after) {
// Updating existing document : Do nothing
return;
} else if (!change.after) {
// Deleting document : subtract one from count
db.doc(docRef).get().then(snap => {
db.doc(docRef).update({numberOfDocs: snap.numberOfDocs - 1});
return;
});
}
});
But when I add a new document in Firestore, I get the following error:
Function returned undefined, expected Promise or value
Any idea?