【问题标题】:Firebase functions tslint error Promises must be handled appropriatelyFirebase 函数 tslint 错误 必须正确处理承诺
【发布时间】:2019-08-21 08:33:30
【问题描述】:

我正在使用 TypeScript 编写一个 firebase 函数来向多个用户发送推送通知。但是当我运行firebase deploy --only functions 命令时,TSLint 会给出错误“必须正确处理承诺”。

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

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

export const broadcastJob = functions.https.onRequest((request, response) => {
    const db = admin.firestore();
    db.collection('profiles').get().then(snapshot => {
        snapshot.forEach(doc => {
            const deviceToken = doc.data()['deviceToken'];
            admin.messaging().sendToDevice(deviceToken, { //<-- Error on this line
                notification: {
                    title: 'Notification',
                    body: 'You have a new notification'
                }
            });
        });
        response.send(`Broadcasted to ${snapshot.docs.length} users.`);
    }).catch(reason => {
        response.send(reason);
    })
});

【问题讨论】:

  • 您忽略了从 sendToDevice 返回的承诺。
  • 我也试过在sendToDevice函数后添加catch然后阻塞,还是报错。
  • 也许您可以编辑问题以显示该代码?你现在展示的肯定是不对的。
  • 请同时包含您的 TSLint 和 TypeScript 版本以及 TSLint 抱怨的哪一行(以便我们知道您发布的代码中的哪一行)。

标签: typescript firebase google-cloud-functions tslint


【解决方案1】:

首先要说一句,我认为您最好使用可调用函数而不是 onRequest。 见:Are Callable Cloud Functions better than HTTP functions?

接下来,您需要等待异步函数完成,然后再发回响应。

在这种情况下,您将遍历查询返回的所有文档。对于您调用 sendToDevice 的每个文档。这意味着您正在并行执行多个异步函数。

你可以使用:

Promise.all([asyncFunction1, asyncFunction2, ...]).then(() => {
 response.send(`Broadcasted to ${snapshot.docs.length} users.`);
});

以下代码未经测试:

export const broadcastJob = functions.https.onRequest((request, response) => {
    const db = admin.firestore();
    db.collection('profiles').get().then(snapshot => {
        Promise.all(snapshot.docs.map(doc => {
            const deviceToken = doc.data()['deviceToken'];
            return admin.messaging().sendToDevice(deviceToken, {
                notification: {
                    title: 'Notification',
                    body: 'You have a new notification'
                }
            });
        })).then(() => {
         response.send(`Broadcasted to ${snapshot.docs.length} users.`);
        }
    }).catch(reason => {
        response.send(reason);
    })
});

请注意,我不使用 snapshot.forEach 函数。

相反,我更喜欢使用 snapshot.docs 属性,该属性包含查询返回的所有文档的数组,并提供所有普通数组函数,例如“forEach”以及我在这里用来转换数组的“map”将文档转换为一组承诺。

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 2019-04-26
    • 2020-03-12
    • 2019-06-13
    • 1970-01-01
    • 2017-10-24
    • 2019-01-24
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多