Bill_The_Coder
Bill_The_Coder

Reputation: 2510

tokens must be a non-empty array at FirebaseMessagingError.FirebaseError

I can't figure out the issue why my tokens list remains empty! I have a collection of users with a field of role to each document. I am iterating over those documents, to check which document field role is equal to shipper in order to retrieve token. So that I could sendMulticast notifications to each shipper device. My order snapshot also contains the shipper tokens as well I am iterating over users collections to grab token for pushing into the list but both of them aren't pushing data into list. Here's my cloud function code:

exports.notifyShipper= functions.firestore.document('/orders/{documentId}')
.onCreate(async (snap,context) => {
    functions.logger.log('Sending Notifications to the shippers');

    var shipperTokens=[];

    await admin.firestore().collection("users").get().then(snapshot => {

    snapshot.forEach(doc => {
       if(doc.role =='shipper'){
         shipperTokens.push(doc.token);
         functions.logger.log(doc.token);
       }
    });

  });
  Array.prototype.push.apply(shipperTokens, snap.shipperTokens);
  functions.logger.log(snap.shipperTokens.toString());

  //   const tokensOfShippers = [
  //   'd7M5eixYQYCmXgDx1rOmKc:APA91bHUELrCtEIHHcoQnQ9p5SqqpeZbKpUe60OyGPlZ4GZ9cm5wosfTZFoHV7NcDm2-5Sowpck2YCIkpzZ7ku3u6LSk9tODHA54OrawxNvOQqlK-x7W6VE4wsAS5KCtK4JPtg2nRfCg',
  //  'cBPUl8FzTJaQx8Mg_CkPbv:APA91bG0bZu7lD8EAxExTqcnJfZjpl71YdtfjdugwREGo-sO8URbjJILRc_DHY61wKJlnYGWCjbKdieKpvPJd5rtaPYa6rBHo1JQqiToVGs4WGd-2D3Y-bMVIzzU8TIzR772oTAUiyR7',
  //  'cuJAUqqCRKq77UsbBS6kZD:APA91bEDWjSR4z9yUSfFg-FpOPMA3VXsyjJ613Etv5MhpV31nsSP5hhgumWmsoLKGh1_TbP3yd9KDU7vO4DYqW5mJpSjPKzllBuw7I-lEAY6UYasJKQXv9DZt82agPyr7oac-JBC_XMr',
  //  'erYeroRkRM-s9y70l8cFqd:APA91bGzT_nT-kWUBXTBX-aDtN8Ew5ypDpbDFWcQw6O5zo4jFQFnMy2_E4Lt5GYVJXcaudK8u7LhkoQadjlfmxtG9jhIhCCch2o850UJcCqrCiNz7muBwKcp0E-y3MrHz9iAAJZ_4uJ3'
  //   ];
    await admin.messaging().sendMulticast({
         tokens : shipperTokens,
          notification: {
              title: "Hi TELLOO Shipper",
             body: "We recieved an order, please have a look on the dashboard!",
               imageUrl: "https://en.pimg.jp/040/171/400/1/40171400.jpg",
        }
   });
});

This is the complete error that I'm facing in logs:

Error: tokens must be a non-empty array
    at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:44:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:90:28)
    at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:279:16)
    at Messaging.sendMulticast (/workspace/node_modules/firebase-admin/lib/messaging/messaging.js:301:19)
    at /workspace/index.js:391:53
    at processTicksAndRejections (internal/process/task_queues.js:95:5) 

Upvotes: 0

Views: 2118

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

If you look at the Firebase documentation on reading multiple documents, you'll see that it uses the data() function on each DocumentSnapshot to get at the fields of that document.

So your doc.role and doc.token should instead of doc.data().role and doc.data().token.

Upvotes: 1

Related Questions