firebase - Firebase Cloud Functions - 发送电子邮件时出错
问题描述
我需要通过 HTTP 请求向 Firebase Cloud Function 发送电子邮件。我使用 Sparkpost 作为提供程序,但是当我使用 Postman 调用函数时,我收到以下错误:
{
"message": {
"errno": "EAI_AGAIN",
"code": "EAI_AGAIN",
"syscall": "getaddrinfo",
"hostname": "api.sparkpost.com",
"host": "api.sparkpost.com",
"port": "443"
}
}
如果我尝试使用命令调用在本地提供的相同函数firebase serve --only functions
,然后使用 Postman 对其进行测试,我不会收到任何错误。
我的代码如下:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const SparkPost = require('sparkpost');
// Initialize App
admin.initializeApp();
exports.sendSurveyRecap = functions.https.onRequest((req, res) => {
try {
if (req.method !== 'POST') {
return res.status(405).json({
message: `Method ${req.method} is not allowed`,
});
}
const client = new SparkPost('<MY API KEY>');
return client.transmissions.send({
content: {
from: '<MY EMAIL DOMAIN>',
subject: 'Hello from node-sparkpost',
html: '<p>Hello world</p>',
},
recipients: [
{ address: 'foo@example.com' },
],
}).then(() => res.status(200).json({
message: 'Message sent successful',
})).catch(error => res.status(500).json({
message: error,
}));
} catch (error) {
return res.status(500).json({
message: error.message,
});
}
});
那么,为什么当我使用“已部署”的 url 调用云函数时收到该错误,而不是使用“已服务的”url 我没有收到任何错误?
解决方案
推荐阅读
- python - 使用文件中的数据创建字典值列表
- c# - 如何让我的自托管 WCF 服务不尝试保留 localhost 以外的 url?
- c# - 如何在本地空间使用“LookAt”?
- ssl - 无法获取 EC 参数:证书的 subjectPublicKeyInfo 中的命名曲线
- firebase - Cloud Functions Node.js Emulator 无法部署功能 - 找不到模块
- react-native - 语音通话连接路由 React-Native
- c# - 我如何使用 Bootstrap Carousel 作为数据库记录的列表?
- silverstripe - Silverstripe 3:如何在设定的日期取消发布页面?
- c - 快速排序未正确排序长值数组
- c++ - 测试子类是否覆盖基类中的虚函数