Dawn17
Dawn17

Reputation: 8297

Using Firebase Database Trigger onUpdate

I am trying to make a trigger when my ignore field in the Firebase database changes.

const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
// // Create and Deploy Your First Cloud Functions - All runs in the Firebase Server, not in the Browser
// // https://firebase.google.com/docs/functions/write-firebase-functions

exports.dbTest = functions.database.ref('/{uid}/ignore')
    .onUpdate((snap, context) => {

        return snap.ref.child('new').set('dddddd')

      });

Just to make a simple test, I created an onUpdate trigger on the ref /{uid}/ignore which points to something shown on the image below. I was expecting this to triggered when I manually change a field (let's say ignore/endTime but nothing happened. I am new to this, so I am confused how to use it. Please help.

enter image description here

EDIT

The function gets triggered but nothing happens. The log says Function execution took 224 ms, finished with status: 'error' without any helpful log message.

Upvotes: 0

Views: 204

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

You seem to be misreading the signature. The onUpdate() trigger gets called with a Change object. From that object you can get the snapshot before and after the update, and from either of those you can get the reference.

So:

exports.dbTest = functions.database.ref('/{uid}/ignore')
    .onUpdate((change, context) => {

        return change.after.ref.child('new').set('dddddd')

      });

Upvotes: 1

Related Questions