Search code examples
javascriptnode.jsfirebasegoogle-cloud-firestorefirebase-cli

Firestore functions won't trigger on event


Firestore Cloud Functions wont trigger on event, My function wont trigger onCreate, onUpdate, onDelete or onWrite. The JS script gets pushed to firestore sucsessfully.

Database desing:

-|search
    -|searchId
        -|id: string
        -|name: sting
        -|img: string

Rule:

match /search/{searchId}{
    allow read;
    allow write: if request.auth != null;
}


const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

exports.updateIndex = functions.database.ref('/search/{searchId}').onWrite(event => {
    console.log('Working')
}

Solution

  • Since you are using firestore, then the above won't work. The function that you have in the question is for firebase not firestore, change it to this:

    exports.updateIndex = functions.firestore.document('search/{searchId}').onWrite(event => {
    console.log('Working');
    });
    

    more info here:

    https://firebase.google.com/docs/functions/firestore-events