Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以异步方式发送网格邮件_Javascript_Sendgrid - Fatal编程技术网

Javascript 以异步方式发送网格邮件

Javascript 以异步方式发送网格邮件,javascript,sendgrid,Javascript,Sendgrid,const sgMail=require('@sendgrid/mail'); 常数{ 钥匙, }=require('../../config/config'); 异步函数放弃密码(req、res){ 试一试{ //... sgMail.setApiKey(keys.sendGridKey); 常数msg={ 收件人:`${req.body.email}`, 发信人:“没有-reply@example.com', 主题:“忘记密码请求”, 文本:“您收到此电子邮件是因为您要求重置密码”, htm

const sgMail=require('@sendgrid/mail');
常数{
钥匙,
}=require('../../config/config');
异步函数放弃密码(req、res){
试一试{
//...
sgMail.setApiKey(keys.sendGridKey);
常数msg={
收件人:`${req.body.email}`,
发信人:“没有-reply@example.com',
主题:“忘记密码请求”,
文本:“您收到此电子邮件是因为您要求重置密码”,
html:`${token}`,
};
sgMail.send(msg);
res.sendStatus(200);
}捕获(错误){
res.status(500.json)(error.message);
}

}
他们添加了示例来发送异步

代码如下:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com', // Use the email address or domain you verified above
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
//ES6 Old way
sgMail
  .send(msg)
  .then(() => {}, error => {
    console.error(error);

    if (error.response) {
      console.error(error.response.body)
    }
  });
//ES8 - Async example
(async () => {
  try {
    await sgMail.send(msg);
  } catch (error) {
    console.error(error);

    if (error.response) {
      console.error(error.response.body)
    }
  }
})();
const sgMail=require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID\u API\u KEY);
常数msg={
致:'test@example.com',
发件人:'test@example.com“,//使用您在上面验证的电子邮件地址或域
主题:“用Twilio SendGrid发送很有趣”,
text:'在任何地方都很容易,即使使用Node.js',
html:“并且在任何地方都可以轻松操作,即使使用Node.js”,
};
//ES6老路
邮政总局
.send(msg)
。然后(()=>{},错误=>{
控制台错误(error);
if(error.response){
console.error(error.response.body)
}
});
//ES8-异步示例
(异步()=>{
试一试{
等待sgMail.send(msg);
}捕获(错误){
控制台错误(error);
if(error.response){
console.error(error.response.body)
}
}
})();

您是否关心此处的响应?你真的需要检查回复吗?如果没有,那就让它触发并忘记。
这不是以异步方式工作的。
你是什么意思?如果您从未使用
wait
,为什么要使函数
异步?也许如果您使用
.send
返回的承诺,您会得到异步:p。。。快速修复<代码>等待sgMail.send(msg)。。。完成