Ricardo Oscar Kanitz
Ricardo Oscar Kanitz

Reputation: 527

How to fix Value for argument "documentPath" is not a valid resource path?

I'm trying to trigger a function which the token will be accessed on a subcollection, going through documents without specifying it. In dart I could use documentId for it, can I have something similar with javascript? Tried the following but it returns me:

Value for argument "documentPath" is not a valid resource path

Any lights on how to fix it? thanks in advance for your help!

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

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

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



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

return 
 admin.firestore().collection('chat').doc(documentId)
 .collection(documentId).doc(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);
       })
     }

   })
})

I believe my documentId on 'admin.firestore().collection('chat') .doc(documentId).collection(documentId).doc(documentId).collection('messages').' is not returning me what I expected it to do.

Upvotes: 2

Views: 6523

Answers (1)

Ricardo Oscar Kanitz
Ricardo Oscar Kanitz

Reputation: 527

Found my answer on Update Sub-collections on CloudFunctions, just needed to specify:

const chatId = context.params.chatId;
const usersId = context.params.usersId;

Thanks @Frank van Puffelen!

Upvotes: 2

Related Questions