Reputation: 164
I just started using Firebase Cloud Functions and I came accross this source code https://github.com/AngularFirebase/93-fcm-ionic-demo/blob/master/functions/src/index.ts. I already downloaded all the required dependencies for the project but I keep getting an error when using event.data.data(). Here's 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!`,
}
}
// ref 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 error is supposedly on const data= event.data.data(). I'm totally sure this is the correct way to use it, but I just can't deploy my function because of this. I already checked and I have the latest version of cloud functions on both package.json files (the one on my root project folder and the one on the functions folder). Does somebody have any idea of what might be causing this issue? Your help would be very appreciated.
Upvotes: 5
Views: 6861
Reputation: 1
Code example:
const user = userDoc.data();
if (user) {
const { firstName, lastName } = user;
}
You need to check your value isn't undefined
Upvotes: 0
Reputation: 803
i had the same issue. Everything was correct as uptodate but still wont work, i get an error when i deploy. I solved by having "strict": false,
in the tsconfig.json on the "compilerOptions". I hope this helps someone.
Upvotes: 1
Reputation: 80914
The cloud functions have been updated, and the update brought many changes.
Therefore, you need to change the following to this:
exports.newSubscriberNotification = functions.firestore.document('subscribers/{subscriptionId}').onCreate((snap,context) => {
const data = snap.data();
more information here:
https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore
Upvotes: 6