From 0a88a58c3d1214fe6542af33764c5a3bf0da31dd Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 18 Sep 2018 20:09:10 -0700 Subject: [PATCH] Add print statements to firestore function --- functions/firebase/index.js | 16 ++++++++++++---- functions/node8/index.js | 13 +++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/functions/firebase/index.js b/functions/firebase/index.js index aaddedca15..15a158c039 100644 --- a/functions/firebase/index.js +++ b/functions/firebase/index.js @@ -43,10 +43,18 @@ exports.helloRTDB = (event, callback) => { exports.helloFirestore = (event, callback) => { const triggerResource = event.resource; - // We're just going to log the resource string and the - // full event to prove that it worked. - console.log(`Function triggered by change to: ${triggerResource}`); - console.log(JSON.stringify(event)); + console.log(`Function triggered by event on: ${triggerResource}`); + console.log(`Event type: ${event.eventType}`); + + if (event.data.oldValue && Object.keys(event.data.oldValue).length) { + console.log(`\nOld value:`); + console.log(JSON.stringify(event.data.oldValue, null, 2)); + } + + if (event.data.value && Object.keys(event.data.value).length) { + console.log(`\nNew value:`); + console.log(JSON.stringify(event.data.value, null, 2)); + } // Don't forget to call the callback. callback(); diff --git a/functions/node8/index.js b/functions/node8/index.js index 70d182a1df..5ab4e86085 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -152,12 +152,17 @@ exports.helloFirestore = (data, context) => { const triggerResource = context.resource; console.log(`Function triggered by change to: ${triggerResource}`); + console.log(`Event type: ${context.eventType}`); - console.log(`\nOld value:`); - console.log(JSON.stringify(data.oldValue, null, 2)); + if (data.oldValue && Object.keys(data.oldValue).length) { + console.log(`\nOld value:`); + console.log(JSON.stringify(data.oldValue, null, 2)); + } - console.log(`\nNew value:`); - console.log(JSON.stringify(data.value, null, 2)); + if (data.value && Object.keys(data.value).length) { + console.log(`\nNew value:`); + console.log(JSON.stringify(data.value, null, 2)); + } }; // [END functions_firebase_firestore_node8]