Audrey Wong
Audrey Wong

Reputation: 53

cloud functions for firebase onwrite not triggering any executions

exports.editData = functions.database.ref('/AllData/hello/A').onWrite((change, context) => {
    const after = change.after;
    if (after.exists()) {
        const data = after.val();
        var value = data;
        // set of data to multiply by turns ratio
        var actualEIn = (value.ein)*200;
        console.log('Data Edited');
    }
    return admin.database().ref('/editedData/hello/A').push({
        ein: actualEIn,
    });
});

Edit: made some edits to the code as suggested! However, when I deploy it there are literally no logs. No logs

Upvotes: 1

Views: 823

Answers (3)

Audrey Wong
Audrey Wong

Reputation: 53

exports.editData = functions.database.ref('/AllData/hello/A/{id}').onWrite((change, context) => {
    const afterData = change.after;
    if (afterData.exists()) {
        console.log('hey');
        const data = afterData.val();
        // set of data to multiply by turns ratio
        var actualEIn = (data.ein)*200;
    }
    return admin.database().ref('/editedData/hello/A').push({
        ein: actualEIn,
    });
});

Hi guys thank you for all your help! :) I managed to solve this by adding a /{id} at the back!

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317808

You've got two things wrong here.

First, newer versions of the firebase-functions SDK since version 1.0 deliver a Change object to onWrite handlers instead of a snapshot, as it appears you are expecting. The Change object has properties for before and after with DataSnapshot objects of the contents of the database before and after the change that triggered the function. Please read the documentation for database triggers to get all the information.

Second, exists() is a method on DataSnapshot, but you're using it on the raw JavaScript object value of the contents of the database the location of change. JavaScript objects coming from val() will not have any methods to call.

You should probably update your code to:

  1. Use the latest version of the firebase-functions module
  2. Alter your function to accept the Change object instead of a snapshot
  3. Use the exists() method on a snapshot in the change, rather than a raw JavaScript object.

Starter code:

exports.editValues = functions.database.ref('/AllData/hello/A').onWrite((change) => {
    const after = change.after;  // the DataSnapshot of the data after it was changed
    if (after.exists()) {
        const data = after.val()  // the raw JavaScript value of the location
        // use data here
    }
})

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80944

Change this:

exports.editValues = functions.database.ref('/AllData/hello/A').onWrite((snapshot) => {
const data = snapshot.val();
if (data.exists()) {

into this:

exports.editValues = functions.database.ref('/AllData/hello/A').onWrite((change,context) => {
const data = change.after.val();
if (data.exists()) {

more info here:

https://firebase.google.com/docs/functions/beta-v1-diff#realtime-database

Upvotes: 2

Related Questions