Getting error while deploying node.js cloud function

I am implementation notifications of firebase in my app. This is my node.js function

'use strict'

const functions = require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotificaiton=functions.database.ref('/Notifications/{userKey}/{notification_id}').
onWrite(event => {
const userKey = event.params.userKey;
const notification = event.params.notification;

console.log('The userKey is ', userKey);

});

My firebase db structure is enter image description here

Error in Functions is

enter image description here

Please help me . Thanks in advance

1 answer

  • answered 2018-11-07 23:25 Gastón Saillén

    Change your code

    From this

    exports.sendNotificaiton=functions.database.ref('/Notifications/{userKey}/{notification_id}').
    onWrite(event => {
    const userKey = event.params.userKey;
    const notification = event.params.notification;
    
    console.log('The userKey is ', userKey);
    
    });
    

    To this

    exports.sendNotificaiton=functions.database.ref('/Notifications/{userKey}/{notification_id}').onWrite((change, context) => {
    
    const userKey = context.params.userKey;
    const notification = context.params.notification;
    
    console.log('The userKey is ', userKey);
    
    });
    

    You are using the old event trigger for onWrite(), now you need to pass context and your dataSnapshot (change).

    Also onWrite has before and after values when a write event triggers your database

    check the docs here : https://firebase.google.com/docs/functions/database-events?hl=en

    See the notification example on github for notifications: https://github.com/firebase/functions-samples/tree/Node-8/fcm-notifications