为什么我的 SendGrid 函数返回 "Service Unavailable"?
Why is my SendGrid function returning "Service Unavailable"?
我是 SendGrid 的新手,正在尝试使用后端的 Firebase 云功能从我的 React.js 应用程序发送电子邮件。
当有人要发送消息时,我会向 Firestore 写一个文档。然后我会收听该集合,当创建文档时,我会从云功能发送一封电子邮件。
我正在尝试使其正常工作,但我的云功能中不断收到错误记录和此错误消息:
Error: Service Unavailable
at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:133:29)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
编辑:我在 catch 语句中捕获了错误,并获得了更多详细信息。这是完整的日志:
{ Error: Forbidden
at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:133:29)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
code: 403,
message: 'Forbidden',
response:
{ headers:
{ server: 'nginx',
date: 'Thu, 11 Jun 2020 17:03:27 GMT',
'content-type': 'application/json',
'content-length': '281',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' },
body: { errors: [Array] } } }
编辑 2:
所以我已经按照错误 https://sendgrid.com/docs/for-developers/sending-email/cors/
中的 link 如果我做对了,它表明它阻止了我,因为我正在尝试对他们的 API 进行基于浏览器的调用?但事实并非如此。我正在通过云函数进行调用。
我不知道是不是我做错了什么,还是我只是运气不好,第一次尝试实现它时 SendGrid 遇到了一些问题 (Service Unavailable
)。
我是这样定义 sgMail 的:
import * as sgMail from "@sendgrid/mail";
const SENDGRID_API_KEY = functions.config().sendgridfull.key;
sgMail.setApiKey(SENDGRID_API_KEY);
这是我的云函数,我检查了所有的变量,它们都存在。
exports.firestoreEmail = functions.firestore
.document("email_messages/{messageID}")
.onCreate((snap, context) => {
const msgData = snap.data();
if (!msgData) return;
return db
.collection("users")
.doc(msgData.recipient_ID)
.get()
.then((doc) => {
const recipientData = doc.data();
if (!recipientData) return;
const promises: any = [];
const msg = {
to: recipientData.email,
from: msgData.sender_email,
templateId: "d-myRandomTemplateId",
dynamic_template_data: {
sender_name: "Some One",
},
};
const msg2 = {
to: msgData.sender_email,
from: recipientData.email,
templateId: "d-myRandomTemplateId",
dynamic_template_data: {
sender_name: "Some One",
},
};
promises.push(sgMail.send(msg));
promises.push(sgMail.send(msg2));
return Promise.all(promises);
});
});
请检查此 github issue,其中指定如何使用 sgMail.send 方法,该方法导致 Cloud Function return 服务不可用错误。
send 和 sendMultiple 方法return 一个 Promise,因此您可以处理成功并捕获错误:
sgMail
.send(msg)
.then(() => {
// Celebrate
})
.catch(error => {
// Log friendly error
console.error(error);
if (error.response) {
// Extract error msg
const {message, code, response} = error;
// Extract response msg
const {headers, body} = response;
console.error(body);
}
});
或者,您可以将回调函数作为最后一个参数传递:
sgMail
.send(msg, (error, result) => {
if (error) {
// Do something with the error
}
else {
// Celebrate
}
});
如果有效请告诉我。
我是 SendGrid 的新手,正在尝试使用后端的 Firebase 云功能从我的 React.js 应用程序发送电子邮件。 当有人要发送消息时,我会向 Firestore 写一个文档。然后我会收听该集合,当创建文档时,我会从云功能发送一封电子邮件。
我正在尝试使其正常工作,但我的云功能中不断收到错误记录和此错误消息:
Error: Service Unavailable
at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:133:29)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
编辑:我在 catch 语句中捕获了错误,并获得了更多详细信息。这是完整的日志:
{ Error: Forbidden
at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:133:29)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
code: 403,
message: 'Forbidden',
response:
{ headers:
{ server: 'nginx',
date: 'Thu, 11 Jun 2020 17:03:27 GMT',
'content-type': 'application/json',
'content-length': '281',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' },
body: { errors: [Array] } } }
编辑 2:
所以我已经按照错误 https://sendgrid.com/docs/for-developers/sending-email/cors/
中的 link 如果我做对了,它表明它阻止了我,因为我正在尝试对他们的 API 进行基于浏览器的调用?但事实并非如此。我正在通过云函数进行调用。
我不知道是不是我做错了什么,还是我只是运气不好,第一次尝试实现它时 SendGrid 遇到了一些问题 (Service Unavailable
)。
我是这样定义 sgMail 的:
import * as sgMail from "@sendgrid/mail";
const SENDGRID_API_KEY = functions.config().sendgridfull.key;
sgMail.setApiKey(SENDGRID_API_KEY);
这是我的云函数,我检查了所有的变量,它们都存在。
exports.firestoreEmail = functions.firestore
.document("email_messages/{messageID}")
.onCreate((snap, context) => {
const msgData = snap.data();
if (!msgData) return;
return db
.collection("users")
.doc(msgData.recipient_ID)
.get()
.then((doc) => {
const recipientData = doc.data();
if (!recipientData) return;
const promises: any = [];
const msg = {
to: recipientData.email,
from: msgData.sender_email,
templateId: "d-myRandomTemplateId",
dynamic_template_data: {
sender_name: "Some One",
},
};
const msg2 = {
to: msgData.sender_email,
from: recipientData.email,
templateId: "d-myRandomTemplateId",
dynamic_template_data: {
sender_name: "Some One",
},
};
promises.push(sgMail.send(msg));
promises.push(sgMail.send(msg2));
return Promise.all(promises);
});
});
请检查此 github issue,其中指定如何使用 sgMail.send 方法,该方法导致 Cloud Function return 服务不可用错误。
send 和 sendMultiple 方法return 一个 Promise,因此您可以处理成功并捕获错误:
sgMail
.send(msg)
.then(() => {
// Celebrate
})
.catch(error => {
// Log friendly error
console.error(error);
if (error.response) {
// Extract error msg
const {message, code, response} = error;
// Extract response msg
const {headers, body} = response;
console.error(body);
}
});
或者,您可以将回调函数作为最后一个参数传递:
sgMail
.send(msg, (error, result) => {
if (error) {
// Do something with the error
}
else {
// Celebrate
}
});
如果有效请告诉我。