How to trigger Cloud Function only when child is added to the database?

678 views Asked by At

How can I modify the code below so that the Function only triggers when a child is added? Currently, the Function is triggered everytime there's a change in the specified reference...

const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);



exports.myFunction = functions.database.ref('/messaging/{pushId}')

.onWrite(event => {

//child is added. Do something!

});
1

There are 1 answers

0
Bob Snyder On BEST ANSWER

Use the onCreate() handler instead of onWrite().

exports.myFunction = functions.database.ref('/messaging/{pushId}')

.onCreate(event => {

//child is added. Do something!

});