我尝试通过POST请求发送表单:
客户端
angular.module('app').controller('contactCtrl', function($scope, $http) {
$scope.envoyer = function(nom, organisation, courriel, telephone, message){
$http.post('/contact', {nom:nom, organisation:organisation, courriel:courriel, telephone:telephone, message:message}).then(function(error, response){
console.log('sent!');
if(response.data.success){
console.log('sent!');
}
});
}
});
服务器端
app.post('/contact', function(req, res, next){
var subject = req.query.nom;
var nom = 'Nom complet: ' + req.query.nom + '<br/>';
var organisation = 'Organisation: ' + req.query.organisation + '<br/>';
var courriel = 'Courriel: ' + req.query.courriel + '<br/>';
var telephone = 'Téléphone: ' + req.query.telephone + '<br/>';
var body = 'Message: ' + req.query.message.replace('/(?:\r\n|\r|\n)/g', '<br/>'); // THE ERROR TRIGGERS HERE !!!
var content = nom + organisation + courriel + telephone + body;
console.log(subject);
console.log(content);
mailer.sendMail('aaa@gmail.com', subject, content, next);
});
邮件收发器
var mailer = require('nodemailer');
var EMAIL_ACCOUNT_EMAIL = 'aaa@gmail.com';
var EMAIL_ACCOUNT_PASSWORD = 'aaaa';
var smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: EMAIL_ACCOUNT_EMAIL,
pass: EMAIL_ACCOUNT_PASSWORD
}
});
exports.sendMail = function(toAddress, subject, content, next){
var success = true;
var mailOptions = {
to: toAddress,
subject: subject,
html: content
};
smtpTransport.sendMail(mailOptions, function(error, res){
if(error){
console.log('[ERROR] Message NOT sent: ', error);
success = false;
} else {
console.log('[INFO] Message sent: ' + res.message);
}
next(error, success);
});
}
但是,我在标记的行上的node.js控制台上收到错误:
TypeError:无法调用方法&#39;替换&#39;未定义的
有什么好主意需要我来解决这个问题吗?
答案 0 :(得分:0)
您有此错误消息,因为req.query.message未定义。检查变量req.query.message是否存在,这应该有效。