I am currently trying to debug my firebase functions to trigger notifications and I found out that i can do that with firebase cli by using this command
firebase emulators:start
For some reason, when I call my function on the localhost:5001 I get this error
Cannot read properties of undefined (reading 'toString')
and in the terminal I get this
! functions: TypeError: Cannot read properties of undefined (reading 'toString')
at C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:688:54
at Layer.handle [as handle_request] (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:149:13)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:145:7)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:145:7)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:145:7)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:145:7)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:145:7)
at next (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:145:7)
at Route.dispatch (C:\Users\mrxbu\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\route.js:119:3)
! Your function was killed because it raised an unhandled error.
Am I doing something wrong?
Function:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://console.firebase.google.com/project/projectname",
});
exports.sendNotificationOnFighterDataChangeToFans = functions.firestore
.document("users/{userId}")
.onUpdate(async (change, context) => {
const before = change.before.data(); // Data before the change
const after = change.after.data(); // Data after the change
// Check if userName changed
if (before.weightClass !== after.weightClass) {
const userId = context.params.userId;
const fighterSnapshot = await admin
.firestore()
.collection("users")
.doc(userId)
.get();
const fighterData = fighterSnapshot.data();
if (fighterData && fighterData.route === "fighter") {
const followers = fighterData.followers || [];
const fanPromises = followers.map(async (followerId) => {
const fanSnapshot = await admin
.firestore()
.collection("users")
.doc(followerId)
.get();
const fanData = fanSnapshot.data();
if (fanData && fanData.route === "fan" && fanData.deviceToken) {
const notification = {
token: fanData.deviceToken,
notification: {
title: "UserName Changed",
body: `Your userName has been changed to ${after.userName}`,
},
};
try {
await admin.messaging().send(notification);
console.log("Notification sent successfully");
} catch (error) {
console.error("Error sending notification:", error);
}
}
});
await Promise.all(fanPromises);
}
}
});
localhost url structure
http://localhost:5001/projectname-83750/us-central1/sendNotificationOnFighterDataChangeToFans-0