0

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?

Kato
  • 40,352
  • 6
  • 119
  • 149
Paco Zevallos
  • 2,165
  • 7
  • 30
  • 68

1 Answers1

3

Firestore triggers should return a promise the resolves when all asynchronous work is complete, or null if there was no work. Each one of your three cases needs to have a proper return value.

    if (!change.before) {
        // New document Created : add one to count
        return db.doc(docRef).get().then(snap => {
            return db.doc(docRef).update({numberOfDocs: snap.numberOfDocs + 1});
        });
    } else if (change.before && change.after) {
        // Updating existing document : Do nothing
        return null;
    } else if (!change.after) {
        // Deleting document : subtract one from count
        return db.doc(docRef).get().then(snap => {
            return db.doc(docRef).update({numberOfDocs: snap.numberOfDocs - 1});
        });
    }

If you're going to spend time working with Cloud Functions, you should definitely learn how promises work. There is a three part video tutorial that starts here: https://www.youtube.com/watch?v=7IkUgCLr5oA&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=1

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks @Doug Stevenson this worked and made the function not errors. A question how do I print the total of the documents in my html component? I'm calling `{{numberOfDocs}}` but I do not get any results or how do I see that topic ?. Thanks again and I'll watch the video anyway. – Paco Zevallos May 05 '18 at 21:08
  • If you have a followup question, please ask that separately. – Doug Stevenson May 05 '18 at 21:12
  • It's okay. Thanks for your help. – Paco Zevallos May 05 '18 at 21:15