首页 > 解决方案 > 参数结果隐式具有任何类型

问题描述

在此处输入图像描述我有一个聊天应用程序,我正在尝试为所有订阅者发送通知。我必须遍历用户以使用 where 子句中的 ID 来获取他们的设备令牌

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);
    });

不知道为什么结果有这个错误,但它不会让我上传到云功能。任何帮助将不胜感激。

标签: javascripttypescriptgoogle-cloud-firestoregoogle-cloud-functions

解决方案


看起来这可能是一个 tslint 问题。您可能需要将类型声明为“任何”不允许隐式。它必须是明确的。

试试这个:

devices.forEach((result: any) => {

推荐阅读