Firebase functions: cannot read property 'user_id' of undefined
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
andonUpdate
events, the data parameter hasbefore
andafter
fields. Each of these is aDataSnapshot
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