javascript - Firebase 功能失败
问题描述
我无法让 Firebase Cloud Function 正确执行,我很确定这是由于我的 Javascript 能力有限。
功能相当简单;它所做的只是通过 FCM 发送通知。我尝试遵循建议并包含 .send() 行以避免最终超时。但是,根据日志,该函数仅返回状态“500”,并在通过 HTTP 调用时显示“错误”。已return null;
添加,否则 Eslint 进程会引发error Each then() should return a value or throw promise/always-return
问题。
我究竟做错了什么?
const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.https.onRequest((request, response) =>{
var topic = 'all';
var payload = {
notification:{
title: 'Test Title',
body: 'Test body'
},
topic: topic
};
admin.messaging().send(payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
response.status(200).send("ok")
return null;
})
.catch((error) => {
console.log('Error sending message:', error);
response.status(500).send("bad")
});
});
控制台日志:
8:34:57.492 AM sendNotification Successfully sent message: projects/xx
8:34:57.494 AM sendNotification Error sending message: TypeError: response.status is not a function
8:34:57.494 AM sendNotification at /workspace/index.js:55:18
8:34:57.494 AM sendNotification at processTicksAndRejections internal/process/task_queues.js:97:5)
8:34:57.497 AM sendNotification Function execution took 798 ms, finished with status code: 500
解决方案
推荐阅读
- html - 如何仅使用 HTML 根据屏幕大小调整图像大小?
- flutter - 路线“/”的构建器返回 null
- flutter - Flutter Web 波斯语/阿拉伯语字符串在 UI 上未正确显示
- python - 在 pygame 中检查歌曲是否已播放完毕
- python - 如何在 conda 中创建一个单独的 python 2.7 环境?
- javascript - 使用three.js设置透明背景
- c# - CollectionView 在 Not MVVM 后面的代码中延迟加载
- algorithm - 这个方法是 BigO(n^2) 还是 BigO(nLog(n))?
- office-scripts - 工作表打开时运行 Excel Web Office 脚本
- rust - 如何修复actix_web中的“特征工厂<_,_,_>没有为{function}实现”错误?