【发布时间】:2020-04-13 19:23:39
【问题描述】:
我正在设计一个使用 React 呈现 UI 的联系人页面。我有一个表格,应该在提交时发送电子邮件。下面是处理提交的 UI 代码:
handleSubmit = (event) => {
event.preventDefault();
this.setState({
disabled: true
});
Axios.post('http://localhost:3040/api/email', this.state)
.then( res => {
if(res.data.success){
this.setState({
disabled: false,
emailSent: true
});
} else{
this.setState({
disabled: false,
emailSent: false
});
}
})
.catch(err => {
this.setState({
disabled: false,
emailSent: false
});
});
}
发送电子邮件的 api 是用 Node.js 编写的。使用@sendgrid//mail 来触发发送。在调试时,我可以看到表单值正在到达 api,但在发送时会引发 403 Forbidden 错误。 api代码如下:
app.post('/api/email', (req, res, next) => {
sendGrid.setApiKey('<Generated key in sendgrid>');
const msg = {
to: 'some@email.com',
from: req.body.email,
subject: 'Website Contact Page',
text: req.body.message
}
sendGrid.send(msg).then(result => {
res.status(200).json({
success: true
});
})
.catch(err => {
console.log('error: ', err);
res.status(401).json({
success: false
});
});
});
以下是我在调试时在 VSCode 控制台中得到的错误跟踪:
stack:"Error: Forbidden
at axios.then.catch.error (c:\react\portfolio-api\node_modules\@sendgrid\client\src\classes\client.js:105:29)
at process._tickCallback (internal/process/next_tick.js:68:7)"
proto:错误 {constructor: , toString: , toJSON: }
不知道为什么它给我禁止错误。如果我需要在此处添加更多信息,请告诉我。在此先感谢:)
编辑:- 按照 sendgrid 的文档创建 API 密钥并在 sendGrid.setApiKey() 中使用相同的密钥。
【问题讨论】:
-
您的 Sendgrid API 密钥似乎无效。
-
交叉检查了 api 密钥,它们没问题。编辑了从我获取 api 密钥的地方添加图像的问题。
标签: node.js reactjs express sendgrid