functions: TypeError: Cannot read property 'data' of undefined

590 Views Asked by At

I am new with firebase functions and I tried to create push notifications using FCM and firestore triggers, but I am always getting an error. I sent a push notification using https, but never worked using firestore triggers. I tried in many different ways (I even reinstalled firebase-tools) but it did not work. Here is my code. I replaced some irrelevant informations with '------'.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp();

//const db = admin.firestore();
const fcm = admin.messaging();

export const sendMessage = functions.firestore.document('cards/{cardsID}').onWrite(async event =>{
    const registrationToken = '----------';

    const payload: admin.messaging.MessagingPayload = {
        notification: {
            title: "test",
            body: "test"
        }
    };

    return fcm.sendToDevice(registrationToken, payload);
});

The error I always get is:

!  functions: TypeError: Cannot read property 'data' of undefined
    at C:\Users\Cosmin\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:554:28
    at Generator.next (<anonymous>)
    at C:\Users\Cosmin\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\Cosmin\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:4:12)
    at processBackground (C:\Users\Cosmin\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:551:12)
    at C:\Users\Cosmin\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:647:23
    at Generator.next (<anonymous>)
    at C:\Users\Cosmin\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:8:71
    at new Promise (<anonymous>)
!  Your function was killed because it raised an unhandled error.

My thoughts are that something is wrong with firebase-tools or firebase. Thank you in advance for your support!

0

There are 0 best solutions below