javascript - 无法将 nodemailer 错误响应从 node.js 发送到 angular.js

标签 javascript angularjs node.js nodemailer

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

client

    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)
           );
  });

    });

server

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'
     })
}

Client : 在这里你可以通过

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

关于javascript - 无法将 nodemailer 错误响应从 node.js 发送到 angular.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36123246/

相关文章:

javascript - 使用 JSON 数据填充 Javascript 数组

node.js - 在 Ubuntu 上安装 Node.js

JavaScript 电子邮件链接

javascript - offset() 在移动浏览器中返回不同的值

Javascript bool 运算不起作用

javascript - ngRoute 不工作 - AngularJS 1.6.6

javascript - 使用 module.exports 通过脚本使用变量不起作用

javascript - 当用户返回页面时,如何使用 jquery 使所选菜单保持事件状态?

javascript - 在 Javascript 中转换对象格式

angularjs - 声明 2 个 Controller 在单个页面中使用