javascript - 用于 firebase 的 node.js 中的 messaging() 问题

标签 javascript node.js firebase firebase-cloud-messaging google-cloud-functions

我想组织发送有关将文档添加到 Firestore 的推送通知。我正在使用 node.js 的 firebase 站点示例中的代码。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var message = {
    notification: {
        title: 'title!',
        body: 'body'
    },
    topic: "all"
};


exports.createRequest = functions.firestore
    .document('Requests/{RequestsId}')
    .onCreate((snap, context) => {
        console.log('We have a new request');
        // Send a message to devices subscribed to the provided topic.
        admin.messaging().send(message)
            .then((response) => {
                console.log('Successfully sent message:', response);
            }).catch((error) => {
                console.log('Error sending message:', error);
            });
        return 0;
    });

当我尝试部署时出现错误:

Each then() should return a value or throw promise/always-return" for string .then((response) => {

最佳答案

改变这个:

admin.messaging().send(message)
.then((response) => {
  console.log('Successfully sent message:', response);
}).catch((error) => {
  console.log('Error sending message:', error);
  });
 return 0;
 });

进入这个:

return admin.messaging().send(message)
.then((response) => {
 console.log('Successfully sent message:', response);
 return null;
}).catch((error) => {
 console.log('Error sending message:', error);
  });
});

您需要正确地终止函数,这样您就可以避免因运行时间过长或无限循环的函数而产生过多的费用。

您可以使用以下方式终止您的函数:

Resolve functions that perform asynchronous processing (also known as "background functions") by returning a JavaScript promise.

Terminate HTTP functions with res.redirect(), res.send(), or res.end().

Terminate a synchronous function with a return; statement.

关于javascript - 用于 firebase 的 node.js 中的 messaging() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205173/

相关文章:

javascript - setTimeout() 只工作 2 次

node.js - 对周围的所有 Node JS 框架/库等感到困惑

android - 如何使用 Firebase 仅使用电话号码和 otp 实现现有用户登录

javascript - 无法在 Heroku 上使用 Node 运行 Express

javascript - 如何在立方体一侧渲染许多图像并像画廊墙一样定义它们的位置?

node.js - Meteor - 连接超时。没有收到心跳

javascript - 如何让 Puppeteer 在客户端与 ReactJS 应用程序一起工作

java - Firebase 云消息推送通知出现错误

android - FirebaseInstanceIdService 已弃用

javascript - 当用户点击内部链接时显示 html 页面的一部分