Referenceerror: documentId is not defined

Asked

Viewed 28 times

0

Hello, I am trying to create a Firebase Function for my flutter app to send notification to the token that is in an undercutting without specifying the Document. In Dart I can use "documentId", but in the javascript function returned me "Referenceerror: documentId is not defined". How do I access this undercutting without specifying the document? Thanks in advance.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

var msgData;


exports.chatTrigger = functions.firestore.document(
     'chat/{chatId}/users/{usersId}/messages/{messagesId}'
).onCreate((snapshot, context) => {
   msgData = snapshot.data();

return 

  admin.firestore().collection('chat').document(documentId)
  .collection('users').document(documentId)
  .collection('messages').get().then((snapshots) => {
     var tokens = [];
     if (snapshots.empty) {
        console.log('No Devices');
        return false;


     } else {
      for (var token of snapshots.docs) {
          tokens.push(token.data().token);
      }

      var payload = {
         "notification": {
             "title": msgData.name,
             "body": msgData.title,
             "sound": "default"
          },
          "data": {
             "sendername": msgData.name,
             "message": msgData.title,
         }
       }

       return admin.messaging().sendToDevice(tokens, payload).then((response) => {
          console.log('Pushed them all');
       }).catch((err) => {
           console.log(err);
       })
     }

   })
})

1 answer

0


The variable documentId does not seem to have been declared anywhere in your script, so you did not create such a variable, so it will give ReferenceError even

What you want is probably this:

admin.firestore.FieldPath.documentId()

As documented: https://firebase.google.com/docs/reference/admin/admin.firestore.FieldPath#. documentId

You could set at the beginning of the script:

var msgData;
var documentId = admin.firestore.FieldPath.documentId();

...
  • Thanks William, really did not know, so I need to reference the way to the document?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.