angular - 为 Cloud Functions 执行函数部署时出错
问题描述
我正在尝试在下面部署以下功能,但发生错误,我无法识别问题。
在index.js文件中的代码下方。
const functions = require('firebase-functions');
const admin = require("firebase-admin");
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.fcmSend = functions.database.ref('/messages/{userId}/{messageId}').onCreate(event => {
const message = event.after.val();
const userId = event.params.userId;
const payload = {
notification: {
title: message.title,
body: message.body,
icon: "https://placeimg.com/250/250/people"
}
};
return Promise.all([]);
admin.database().ref(`/fcmTokens/${userId}`).once('value')
.then(token => {
token.val();
})
.then(userFcmToken => {
return admin.messaging().sendToDevice(userFcmToken, payload);
})
.then(res => {
console.log("Sent Successfully", res);
})
.catch(err => {
console.log(err);
});
});
显示以下错误:
CMD 中的错误:
27:15 error Each then() should return a value or throw promise/always-return
✖ 1 problem (1 error, 0 warnings)
解决方案
错误消息是说您没有返回then
回调值。您有两个没有返回值(或抛出异常)。在您的代码中查看我的评论:
admin.database().ref(`/fcmTokens/${userId}`).once('value')
.then(token => {
token.val(); // this is not returning a value
})
.then(userFcmToken => {
return admin.messaging().sendToDevice(userFcmToken, payload);
})
.then(res => {
console.log("Sent Successfully", res); // this is not returning a value
})
.catch(err => {
console.log(err);
});
更糟糕的是,你在任何代码被执行之前就返回了:
return Promise.all([]);
推荐阅读
- json - 使用 pymongo 从 mongodump JSON 向 MongoDB 插入文档
- python - 使用 dynamodb 时如何为 Flask-Login 创建用户类?
- python - Pandas,验证数据,检查所有组的长度是否相同
- node.js - Windows 10 上的 Laravel 开发 - 无法识别节点
- spring - 如何在 Spring Boot + Spring Security 中添加一些身份验证步骤?
- vb.net - 将数据从 DataGrid 视图复制到另一个 B
- python - 将 Excel 格式化为 DataFrame
- error-handling - RxJS 重试然后 catchError 不起作用
- tomcat - 在本地主机上发送邮件有效,但是当部署到网络服务器时,我得到无法连接到主机,端口:smtp.gmail.com,587
- ios - watchOS:获取 EventKit 日历 - 不止启用