无法将节点邮件程序错误响应从节点.js发送到角度.js

Unable to send nodemailer error response from node.js to angular.js

本文关键字:js 节点 错误 程序 响应      更新时间:2023-09-26

在这里,我在提交表单时发送邮件。一切正常,唯一的问题是即使由于错误的身份验证或互联网问题而未发送邮件,用户也会收到成功消息。我希望如果邮件发送失败,用户应该收到错误消息。

客户

    this.mail= function() {
    var data = ({
        name :this.name,
        email :this.email
    })
    //Post Request
      $http({
  method: 'POST',
  url: '/contact2', data
}).then(function successCallback(response) {
    $mdToast.show(
                  $mdToast.simple()
                    .textContent('Thanks for your message '+data.name)
                    .position($scope.getToastPosition())
                    .hideDelay(5000)
                );
  }, function errorCallback(response) {
    $mdToast.show(
             $mdToast.simple()
               .textContent('Something went wrong, Please TRY AGAIN '+data.name)
               .position($scope.getToastPosition())
               .hideDelay(5000)
           );
  });
    });

服务器

function send(req, res) {
  console.log(req.body);
  var data= req.body
  smtpTransport.sendMail({ 
     from: "<email@gmail.com>", 
     to: data.email, 
     subject: "Website Submission from "+data.name,
     text: 'You have a new submission with the following details...,
  }, function(error, response){  //callback
     if(error){
         console.log(error);
     }if{
         console.log(" Message sent "+data.name);
     }
     smtpTransport.close(); 
  });
  res.json(data);
}

服务器:您可以从服务器创建错误消息并将其发送

if(error){ 
     res.json({ 
      Status : false,
      message : error
     })
}else{ 
     res.json({ Status : true,
     message : 'Success'
     })
}

客户端 :在这里你可以通过以下方式捕获它

function successCallback(response) {
if(response.Status){ 
     console.log(response.message);
}// for success
else{ 
     console.log(response.message) 
} //for error

}

您可以设置和发送其他data.emailSent = truedata.emailSent = false

if(error){
       data.emailSent = false;
} else{
       data.emailSent = true;
}

在客户端上,您可以检查此标志并相应地显示成功或失败。

感谢Zeeshan Hassan和pooja帮助我,起初我无法访问状态和消息。我刚刚对Pooja的解决方案及其工作进行了这几处更改

function successCallback(response) {
       console.log(response.data.Status);
       if (response.data.Status){console.log(response.data.message);}
`     `else{ console.log(response.data.message) }