gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:57:37 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com