3

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!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Cosmin
  • 31
  • 3
  • I'm not into firebase, but I think that two might be the things: or there's something wrong with the library or with your usage of it (and that is a case that is not handled in the library). – Alexander Cerutti Oct 22 '20 at 10:48
  • 2
    The error message (Cannot read property 'data' of undefined) means that you call a `data` property of an Object (which is undefined). **Nowhere in your code we see `data`**. Did you remove some parts? Also, note that since Version 1.0.0 of the Firebase SDK for Cloud Functions, `firebase-admin` is now initialized without any parameter: `admin.initializeApp();`. See https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin – Renaud Tarnec Oct 22 '20 at 12:38
  • I did not removed any parts of the code. If you're following the paths in the error, no one lead to my **index.ts**. I tried `admin.initializeApp();`, but did not work. I "debugged" some code and the problem appear just when I use the trigger. If I remove all the code and I just do export (eventually with 'return' or not), I have the same error. If I try to use `functions.https`, it works. As @AlexanderCerutti said, I think the problem is the library, but a reinstall is useless – Cosmin Oct 22 '20 at 13:19
  • If you think you uncovered a bug in the Firebase emulators, I'd recommend filing a bug on the [`firebase-tools repo](https://github.com/firebase/firebase-tools). The folks working on that product are much more likely to see it there. – Frank van Puffelen Oct 22 '20 at 13:52

0 Answers0