无法将节点邮件程序错误响应从节点.js发送到角度.js
Unable to send nodemailer error response from node.js to angular.js
在这里,我在提交表单时发送邮件。一切正常,唯一的问题是即使由于错误的身份验证或互联网问题而未发送邮件,用户也会收到成功消息。我希望如果邮件发送失败,用户应该收到错误消息。
客户
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 = true
或data.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) }
相关文章:
- 如何处理node.js节点mongodb中的连接和查询队列
- 更新从Mysql查询检索到的数据?Node.js节点mysql
- Dracula Graph js节点点击事件
- 如何公开JS节点模块
- Vis.js节点上的动画牛眼
- 添加“;三角形向上”;在d3.js节点中
- 验证请求参数的最佳方法是帆.js/节点.js
- 如何在新窗口中打开 Arbor.js 节点链接
- D3.js节点映射中的缩放功能
- 如何围绕焦点旋转 d3.js 节点
- 问.js节点承诺.“套接字”上缺少错误处理程序.类型错误:无法调用未定义的方法“then”
- 将数据返回到索引.js节点表达式
- 如何添加属性以 go.js 节点和重定向
- Js 节点 - socket.io 聊天修饰符
- 如何使用 JSON 图像作为 D3.js 节点背景
- 条件语句在 JS(节点)中没有响应
- Sigma JS节点动画
- 更改d3.js节点的填充颜色
- Arbor Js-节点单击
- 循环中的JS节点