9

I am trying to do a simple hello world firebase function with my mobile app, I want to log the user ID so I can see that the function does work. This is my current javascript code:

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => { console.log('Testing stuff', event.params.user_id); return; }); 

It does trigger when new data is written to specific databasetable but this error shows up:

TypeError: Cannot read property 'user_id' of undefined at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:8:44) at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:700:26 at process._tickDomainCallback (internal/process/next_tick.js:135:7) 

The notification database looks like this: enter image description here

1
  • 2
    Note to everyone who landed here: It's likely that you're using an old version of some tutorial that hasn't been updated to use the API for the Cloud Functions for Firebase API version 1.0. Please notify the creators of the tutorial that their content is out of date. Commented Jun 25, 2018 at 15:41

1 Answer 1

21

You need to install the latest firebase-functions and firebase-admin:

npm install firebase-functions@latest firebase-admin@latest --save npm install -g firebase-tools 

to be able to use the new API, check here for more info:

https://firebase.google.com/docs/functions/get-started#set_up_and_initialize

Change this:

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((event) => { console.log('Testing stuff', event.params.user_id); 

into this:

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.sendNotification = functions.database.ref('/notifications/{user_id}').onWrite((change, context) => { console.log('Testing stuff', context.params.user_id); 

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


params

An object containing the values of the wildcards in the path parameter provided to the ref() method for a Realtime Database trigger.

more info here:

Cloud functions v1.0 Changes

EventContext#params

Change

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.