My goal is to deploy a basic function on Firebase that sends notifications to subscribers when a new record is added to the 'todos' collection.
The token for the subscriber is:
evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_
I tried following a tutorial to achieve this, but I encountered an error during deployment.
Here is the code snippet of my function:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
exports.newSubscriberNotification = functions.firestore
.document('todos')
.onCreate(async event=>{
const data = event.data();
const workdesc = data.workdesc;
const subscriber = "evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_";
// notification content
const payload = {
notification: {
title: 'new write in collection todos',
body: workdesc,
//body: 'body',
icon: 'https://img.icons8.com/material/4ac144/256/user-male.png'
}
}
// send notification
return admin.messaging().sendToDevice(subscriber, payload)
});
Error Details:
src/index.ts:11:11 - error TS6133: 'workdesc' is declared but its value is never read.
11 const workdesc = data.workdesc;
~~~~~~~~
src/index.ts:11:22 - error TS2532: Object is possibly 'undefined'.
11 const workdesc = data.workdesc;
Found 2 errors.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Edit Update:
Even after making the suggested changes, I am still encountering a HTTP Error: 400
while deploying the function. Please refer to the attached screenshot for more information.