【发布时间】: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