【发布时间】:2020-07-14 20:43:39
【问题描述】:
app.post('/users/update',async(req,res)=>{
try{
const user = await User.findOne({email:req.body.email});
if(!user)
{
return res.send({message:"Email ID not registered!"})
}
// SG.K_2ita5rRLWvBO6ind1FzQ.uXUAMa1IzOHV155Sak74blUlmawGeUeRXQ20O5tFA
sgMail.setApiKey('SG.K_2ita5rRLWvBO6ind1FzQ.uXUAMa1IzOHV155Sak74blUlmawGeUeRXQ20O5tFA');
const msg = {
to: user.email,
from: 'kartiken@gmail.com',
subject: 'Validate Your Account',
text: 'Hello User, here is your OTP: ',
html: '<strong>21345</strong>',
};
sgMail.send(msg)
.then(()=>{res.render('forgotPass2',{user})})
.catch((e)=>{console.log('error',e)})
console.log('Check mail')
}catch(e){
res.status(400).send(e)
}
})
我真的厌倦了寻找解决这个问题的方法。邮件永远不会发送,并且每次我做任何事情后都会收到错误消息。错误已被承诺捕获。我在此处附上错误!
Check mail
error ResponseError: Forbidden
at C:\Users\Niranjan\Desktop\krb\web d\node\login-system\node_modules\@sendgrid\client\src\classes\client.js:133:29
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 403,
response: {
headers: {
server: 'nginx',
date: 'Tue, 14 Jul 2020 20:28:08 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] }
}
}
请帮我解决这个问题,以便我可以更进一步......
【问题讨论】:
-
可能错误信息列在 [Array] 中。您是否尝试过读取其中的值?类似: .catch((e)=>{console.log('error',e.body.errors)})
-
我很高兴找到了更好的替代品,即 nodemailer npm 包。这很容易! src:w3schools.com/nodejs/nodejs_email.asp 无论如何谢谢!
标签: node.js sendgrid unhandled