I'm trying to send form via a POST request this way:
client side
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!');
}
});
}
});
server side
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);
});
mailer
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);
});
}
but, i'm getting an error on the node.js console on the marked line:
TypeError: Cannot call method 'replace' of undefined
Any brilliant idea need I to do to fix that, please?
You have this error message because req.query.message is undefined. Check that the variable req.query.message exists and this should work.