0

I'm trying to deploy a simple Firestore Cloud function but am unable to. The compiler isn't telling me what the error is too. Can someone help me out. Some people are saying that the second parameter shouldn't be a wildcard but such a thing makes zero sense.

exports.checkIfScannerExists = functions.firestore.document('Scanner_Number_Check/{push_id}/Phone').onWrite((change, context) => {

    if(change.after.exists())
    {

        const push_id = context.params.push_id;
        const phone_number = change.after.val();

        
        admin.firestore().collection('Store_Logins').where('Phone', '==', phone_number).get()
        .then(snapshot => {
            if(snapshot.empty)
            {
                admin.firestore().collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No")
                return;
            }
            else
            {
                admin.firestore().collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No")
                return;
            }

        })
 
    }
    return null;
})
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Arjun Ram
  • 369
  • 2
  • 18
  • Which error are you encountering? What is the compiler showing?? In addition, note that you are not correctly returning the promises chain. – Renaud Tarnec Jun 25 '20 at 11:22
  • Also, why do you use an `if` if you execute the same operation whatever is the result of the `if` check? – Renaud Tarnec Jun 25 '20 at 11:26
  • Sorry, I botched up the if statement because it was just a basic example. And yes, my promise wasn't returned properly. Thanks! – Arjun Ram Jun 25 '20 at 12:36
  • 1
    Also, it seems that you cannot implement an onWrite function if the number nodes in your path is an odd number. Ref: https://stackoverflow.com/questions/46639058/firebase-cloud-firestore-invalid-collection-reference-collection-references-m – Arjun Ram Jun 25 '20 at 12:50
  • Yes! I didn't catch this one... As said in the [doc](https://firebase.google.com/docs/functions/firestore-events?authuser=1#wildcards-parameters) "Your trigger must always point to a document, even if you're using a wildcard." Answer adapted! – Renaud Tarnec Jun 25 '20 at 12:59

1 Answers1

1

You don't give any error detail, but there are several errors in your Cloud Function:

  • You don't correctly manage the Cloud Function life cycle. Returning a Promise in background Cloud Functions calling asynchronous methods tells the CF platform to wait until the promise is fulfilled or rejected before cleaning up the function. See https://firebase.google.com/docs/functions/terminate-functions for more detail.
  • Your need to adapt the path you use to declare the function: "A trigger must always point to a document, even if you're using a wildcard" (see the doc)
  • You should use data() and not val().
  • Doing db.collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No") will not work. You probably want to do db.collection('Scanner_Number_Check').doc(push_id).set({ Response: "No" })

So, the following should work (untested):

exports.checkIfScannerExists = functions.firestore.document('Scanner_Number_Check/{push_id}').onWrite((change, context) => {

    if (change.after.exists()) {

        const db = admin.firestore();

        const push_id = context.params.push_id;
        //const phone_number = change.after.val();   // There isn't any val() method, this is for the RTDB
        const phone_number = change.after.data().phone_number;

        return db.collection('Store_Logins').where('Phone', '==', phone_number).get()
            .then(snapshot => {
                if (snapshot.empty) {
                    return db.collection('Scanner_Number_Check').doc(push_id).set({ Response: "No" })
                }
                else {
                    // return db.collection('Scanner_Number_Check').collection(push_id).collection('Response').set("No")
                    // You are doing the same thing than above... probably to be adapted
                    return db.collection('Scanner_Number_Check').doc(push_id).set({ Response: "Yes" })
                }
            })

    } else {
        return null;
    }

})
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121