Reputation: 2069
I have a chat app where I'm trying to send notifications for all of the subscribers. I have to iterate through the users to use the ID's in the where clause to grab their device tokens
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
exports.newTopicNotification = functions.firestore
.document('topics/{id}/topic/{doc}/chat/{chat}')
.onWrite( async event => {
const allMessages = event.after.data();
const db = admin.firestore();
let data: any;
if (allMessages) { data = allMessages; }
const title = data ? data.title : '';
const topicId = data ? data.topicId : '';
const groupId = data ? data.groupId : '';
console.log('incomingData', data);
const payload = {
notification: {
title: 'New group topic post',
body: `${title}`
}
};
let users: any = [];
let devices: any = [];
const tokens: any = [];
users = await db.collection('topics')
.doc(`${groupId}`)
.collection('topic')
.doc(`${topicId}`)
.get();
console.log('users', users.data().subscribers);
for (let i = 0; i < users.data().subscribers.length; i++) {
const devicesRef = db.collection('devices').where('userId', '==', users.data().subscribers[i]);
const device = await devicesRef.get();
devices.push(device);
console.log('device', devices);
}
// here the result keeps showing the error
devices.forEach(result => {
const token = result.data().token;
tokens.push(token);
});
return admin.messaging().sendToDevice(tokens, payload);
});
Not sure why result has that error but it won't let me upload to cloud functions as a result. Any help would be greatly appreciated.
Upvotes: 2
Views: 968
Reputation: 4871
Looks like it could be a tslint issue. You may need to declare the type as "any" is not allowed implicitly. It has to be explicit.
Try this:
devices.forEach((result: any) => {
Upvotes: 3