首页 > 解决方案 > 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

标签: javascriptfirebasegoogle-cloud-functionsfirebase-cloud-messaging

解决方案


推荐阅读