ReferenceError: event is not defined firebase function - javascript

I am currently developing appointment application and notification is one of the application. As a result, I have been using firebase function for notifying a user if there is new appointment booked or cancel. I am getting an error that ReferenceError: event is not defined
Node.js code
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{PostKey}/{notification_id}').onWrite((data, context) => {
const user_id = context.params.PostKey;
const notification_id = context.params.notification_id;
console.log('We have a notification from : ', user_id,'this also notification_id',notification_id);
if(!event.data.val())
{
return console.log('A Notification has been deleted from the database : ', notification_id);
}
const deviceToken = admin.database().ref(`/User_Data/${user_id}/Device_Token`).once('value');
console.log('A Notification has been deleted from the database : ', deviceToken);
return deviceToken.then(result => {
const token_id = result.val();
const payload =
{
notification:
{
title:"Appointment has been booked",
body: "one of your Appointmtnt has been booked",
icon:"default"
}
};
return admin.messaging().sendToDevice(token_id,payload).then(response =>
{
return console.log('This was the notification Feature');
});
});
});
Error log
ReferenceError: event is not defined
at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:16:9)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:733:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
**Database structure **

The error message is very precise. It's telling you that you used a variable called event on line 16, but you never defined it:
if(!event.data.val())

Related

How to run 2 firebase cloud functions at a time

I want to run 2 firebase cloud functions at a time I have tried some method but it is throwing an error when functions are triggered. I have 2 childs in the database and when any changes occur in the childs it should show a notification to users. can anyone help me out of this.
This is database image showing 2 childs in database:
this is my index.js file
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const fooModule = require('./foo');
const barModule = require('./bar');
exports.foo = functions.database.ref('app_title').onWrite(fooModule.handler);
exports.bar = functions.database.ref('db_kinda/{Uid}/comment').onWrite(barModule.handler);
this is my foo.js file
const functions = require('firebase-functions');
// Listens for new debate topic created
exports.appTitle = (event) => {
console.log('Push notification event triggered');
const app_title = change.after.val();
if (!change.after.exists() && change.before.exists()) {
return change.before.val();
}
const payload = {notification: {
title: 'New Notification',
body: `${app_title}`
}};
return admin.messaging().sendToTopic("appGlobal",payload)
.then(function(response){
console.log('Notification sent successfully:',response);
})
.catch(function(error){
console.log('Notification sent failed:',error);
});
};
this is my bar.js file
const functions = require('firebase-functions');
exports.data = (event) => {
console.log('Push notification event triggered');
const comment = change.after.val();
if (!change.after.exists() && change.before.exists()) {
return change.before.val();
}
const payload = {notification: {
title: 'New Notification',
body: `${comment}`
}};
return admin.messaging().sendToTopic("appGlobal",payload)
.then(function(response){
console.log('Notification sent successfully:',response);
})
.catch(function(error){
console.log('Notification sent failed:',error);
});
};
This functions are deploying properly but when it runs on firebase functions it throws error like this
TypeError: handler is not a function
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:127:23)
at /worker/worker.js:825:24
at
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
As the error says, handler is not a function. When you import foo for example, you're getting the value of exports which has a property called appTitle, not handler. Try this:
const fooModule = require('./foo');
exports.foo = functions.database.ref('app_title').onWrite(fooModule.appTitle);
Or, change your ./foo export to:
const functions = require('firebase-functions');
// Listens for new debate topic created
exports.handler = (event) => {
...
};

Firebase Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array

i want to send users notification when they are being sent friend request using firebase cloud messaging, but when the request is sent it returns this error in firebase function log
Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
this is the java-script code i am using
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We Have A Notification for :', user_id);
if (!change.after.val()){
return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
}
const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Friend Request",
body: "You just got a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
console.log('this is the notification')
});
});
});
It sounds like token_id is null or an empty string. Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user.
To prevent the error message, simply check if the snapshot exists before using its value:
const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
return deviceToken.then(result => {
if (!result.exists() || result.val() === "") return false;
const token_id = result.val();
const payload = {
notification: {
title: "Friend Request",
body: "You just got a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
console.log('this is the notification')
});
});
after many wasted hours i got to discover what was wrong. now the issue was that i was pointing to the wrong path. this line of code was the issue
const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
it was supposed to be this
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

can't get params from firebase function

I'm currently trying to send a confirmation email with sendgrid API from a firebase function.
The API is not the problem though, it seems to work fine, my problem is that I can't get the child oncreate's value (Firebase function log):
TypeError: Cannot read property 'participant_id' of undefined
at exports.SendEmail.functions.database.ref.onCreate.event (/user_code/index.js:15:38)
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:716:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
And here's my code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.SendEmail = functions.database.ref('participants/{participant_id}').onCreate(event => {
const participant_id = event.params.participant_id;
const db = admin.database();
return db.ref(`participants/${participant_id}`).once('value').then(function(data){
const user = data.val();
const email = {
to: user.email,
from: 'recrutement.cisssme16#ssss.gouv.qc.ca',
subject: "Bienvenue au Blitz d'embauche 2018!",
templateId: 'dd3c9553-5e92-4054-8919-c47f15d3ecf6',
substitutionWrappers: ['<%', '%>'],
substitutions: {
name: user.name,
num: user.num
}
};
return sgMail.send(email)
})
.then(() => console.log('email sent to', user.email))
.catch(err => console.log(err))
});
This is not my first firebase function. I even copied pasted previous working codes which worked fined and I still get an undefined value!
What's the problem here? Did firebase changed event.params?
Also my participant_id is an integer value (ex.: 3827), if that changes something.
Thanks in advance!
The signature of the function is wrong, take a look at this example on Handle event data:
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
console.log('Uppercasing', context.params.pushId, original);
});
So just adjust your onCreate function and you'll be all set :)

Cloud Functions for Firebase onWrite trigger: snapshot.val is not a function

I created few functions in the same index.js file, which is sendEmail, sendEmailByDbStatusChange and sendEmailConfirmation.
sendEmail- To be call via HTTP/API
sendEmailByDbStatusChange - listening to DB while value change, but the action is hardcoded
sendEmailConfirmation- Listing to DB while value change, the action subject to the snapshot.
Below is my codes:
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
// Sends an email confirmation when a user changes his mailing list subscription.
exports.sendEmail = functions.https.onRequest((req, res) => {
if (req.body.subject === undefined || req.body.recipient === undefined) {
// This is an error case, as "message" is required.
//res.status(400).send('subject/body/recipient is missing!');
return false
} else {
const mailSubject = req.body.subject;
const mailHtmlBody = req.body.htmlBody;
const mailRecipient = req.body.recipient;
const mailOptions = {
from: '"Food Ninja." <foodninjaapp#gmail.com>',
to: mailRecipient,
subject: mailSubject,
html: mailHtmlBody
};
//res.status(200).send('Success: ' + mailSubject + ' to ' + mailRecipient);
return mailTransport.sendMail(mailOptions)
.then(() => {
console.log(`${mailSubject}subscription confirmation email sent to: `, mailRecipient)
return res.status(200).send('Success: ' + mailSubject + ' to ' + mailRecipient)
})
.catch((error) => console.error('There was an error while sending the email:', error));
}
});
exports.sendEmailByDbStatusChange = functions.database.ref('/users/{uid}').onWrite((event) => {
//const snapshot = event.data;
//const val = snapshot.val();
//if (!snapshot.changed('subscribedToMailingList')) {
// return null;
//}
const mailSubject = 'Sending email with Cloud Function - by DB onWrite Trigger';
const mailHtmlBody = '<h1>Hello Jerry</h1><p>If you receiving this means that you have successfully deployed a customized firebase function</p><p>Be Happy!<br><br>Food Ninja Team</p>';
const mailRecipient = 'admin#phd.com.my';
const mailOptions = {
from: '"Food Ninja." <foodninjaapp#gmail.com>',
to: mailRecipient,
subject: mailSubject,
html: mailHtmlBody
};
//const subscribed = val.subscribedToMailingList;
// Building Email message.
//mailOptions.subject = subscribed ? 'Thanks and Welcome!' : 'Sad to see you go :`(';
//mailOptions.text = subscribed ? 'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.' : 'I hereby confirm that I will stop sending you the newsletter.';
return mailTransport.sendMail(mailOptions)
.then(() =>
console.log(`${mailSubject}subscription confirmation email sent to: `, mailRecipient)
//return res.status(200).send('Success: ' + mailSubject + ' to ' + mailRecipient)
)
.catch((error) => console.error('There was an error while sending the email:', error));
});
exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite((event2) => {
console.log(event2)
console.log(event2.val())
console.log(event2.val().data)
console.log(event2.data)
console.log(event2.data.val())
const snapshot = event2.data;
console.log(snapshot)
const val = snapshot.val();
console.log(val)
if (!snapshot.changed('subscribedToMailingList')) {
return null;
}
const mailOptions = {
from: '"Spammy Corp." <noreply#firebase.com>',
to: val.email,
};
const subscribed = val.subscribedToMailingList;
// Building Email message.
mailOptions.subject = subscribed ? 'Thanks and Welcome!' : 'Sad to see you go :`(';
mailOptions.text = subscribed ? 'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.' : 'I hereby confirm that I will stop sending you the newsletter.';
return mailTransport.sendMail(mailOptions)
.then(() => console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email))
.catch((error) => console.error('There was an error while sending the email:', error));
});
My problem is, after i deploy the code to firebase function, the console shows that the sendEmailConfirmation unable to execute smoothly due to event2.val is not a function.
My current code combined with my customize code and the original code, which sendEmailConfirmation is the original code. When run the original code independently it did work (original was event instead of event2 for the snapshot).
Please advise.
It looks like you updated to v1.0 of the Firebase SDK for Cloud Functions, but didn't upgrade your code to match.
The entire process is explained in this documentation page. Right now you're being hit by the changes in database triggers, which shows that:
Event data now a DataSnapshot
In earlier releases, event.data was a DeltaSnapshot; now in v 1.0 it is a DataSnapshot.
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. For example:
Before (<= v0.9.1)
exports.dbWrite = functions.database.ref('/path').onWrite((event) => {
const beforeData = event.data.previous.val(); // data before the write
const afterData = event.data.val(); // data after the write
});
Now (v1.0.0)
exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
const beforeData = change.before.val(); // data before the write
const afterData = change.after.val(); // data after the write
});
According to that example, you'll need something along these lines:
exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite((change, context) => {
const snapshot = change.after;
const val = snapshot.val();
console.log(val)
if (!snapshot.changed('subscribedToMailingList')) {
return null;
}
const mailOptions = {
from: '"Spammy Corp." <noreply#firebase.com>',
to: val.email,
};
const subscribed = val.subscribedToMailingList;
// Building Email message.
mailOptions.subject = subscribed ? 'Thanks and Welcome!' : 'Sad to see you go :`(';
mailOptions.text = subscribed ? 'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.' : 'I hereby confirm that I will stop sending you the newsletter.';
return mailTransport.sendMail(mailOptions)
.then(() => console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email))
.catch((error) => console.error('There was an error while sending the email:', error));
});
Since version 1.0.0 of the firebase-functions module, database onWrite events now deliver a Change object rather than a DataSnapshot object as the first parameter. You can read about all the breaking changes in 1.0.0 in the documentation. You should use this change object instead to choose if you want to examine the contents of the database before or after the change that invoked it.

Can't be get any notification when the friend request one user to another user through nodejs in android Firebase Function

I'm trying to create notification simple AI for my chat application when i request form one user id to another user id it doesn't display any notification to other user id and in function there was shown the error as:
Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
at Messaging.validateRegistrationTokensType (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:589:19)
at Messaging.sendToDevice (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:210:14)
at Promise.all.then.result (/user_code/index.js:92:32)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I'm not a good coder of nodejs i referred from some tutorial from google but it doesn't work for me can any one help me why this error will be shown and how to fixed it. also i have no any notification form one to another user id.
Here is my nodejs code:
index.js:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event => {
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
console.log('We have a notification from : ', user_id);
if(!event.data.val()){
return console.log('A Notification has been deleted from the database : ', notification_id);
}
const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
console.log('You have new notification from : ', from_user_id);
const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return Promise.all([userQuery, deviceToken]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();
const payload = {
notification: {
title : "New Friend Request",
body: `${userName} has sent you request`,
icon: "default",
click_action : "com.rotaractnepalapp.rotraconversation_TARGET_NOTIFICATION"
},
data : {
from_user_id : from_user_id
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
return console.log('This was the notification Feature');
});
});
});
});
This is because:
const userName = result[0].val();
const token_id = result[1].val();
one of the above is empty, you need to add console.log() to check which one. Also you need to check your database and path database in ref() to be sure that there is a token.

Categories