Recently, I dove into the world of Firebase Cloud Functions and stumbled upon this source code https://github.com/AngularFirebase/93-fcm-ionic-demo/blob/master/functions/src/index.ts. After ensuring all necessary dependencies were installed for my project, I encountered an error when attempting to use event.data.data(). Here's a snippet of my code:
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
exports.newSubscriberNotification = functions.firestore
.document('subscribers/{subscriptionId}')
.onCreate(async event => {
const data = event.data.data();
const userId = data.userId
const subscriber = data.subscriberId
// Notification content
const payload = {
notification: {
title: 'New Subscriber',
body: `${subscriber} is following your content!`,
}
}
// Reference to the parent document
const db = admin.firestore()
const devicesRef = db.collection('devices').where('userId', '==', userId)
// Get users tokens and send notifications
const devices = await devicesRef.get()
const tokens = []
devices.forEach(result => {
const token = result.data().token;
tokens.push( token )
})
return admin.messaging().sendToDevice(tokens, payload)
The issue seems to be centered around const data= event.data.data(). I'm confident that this is the correct way to utilize it, but I am unable to deploy my function due to this error. I've verified that both package.json files (the one in my root project folder and the one in the functions folder) are up to date with the latest cloud functions version. Any insights on what might be causing this setback would be greatly appreciated.