1
votes

I writed this method which should console.log the trigered node data, but i get error.

This is what I tried"

exports.makeUppercase = functions.database
  .ref('/users/{userId}/matches')
  .onWrite((snapshot, context) => {
    // Grab the current value of what was written to the Realtime Database.
    //const original = snapshot.val();
    console.log('OnWrite works' + snapshot.after.val());
    // const uppercase = original.toUpperCase();
    // You must return a Promise when performing asynchronous tasks inside a Functions such as
    // writing to the Firebase Realtime Database.
    // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
    return null;
  });

This is the error: makeUppercase TypeError: snapshot.val is not a function at exports.makeUppercase.functions.database.ref.onWrite (/srv/index.js:49:44) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

Did I made something wrong?

1

1 Answers

3
votes

From the docs:

Event data now a DataSnapshot.

In earlier releases, event.data was a DeltaSnapshot; from v 1.0 onward it is a DataSnapshot.

For onWrite and onUpdate events, the data parameter has before and after fields. Each of these is a DataSnapshot with the same methods available in admin.database.DataSnapshot.

For example:

exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
  const beforeData = change.before.val(); // data before the write
  const afterData = change.after.val(); // data after the write
});

Therefore in your code, you need to either use the after property to retrieve the after the write or before property:

const original = snapshot.after.val();