Reputation: 540
I'm using nodemailer to send mail which contains all the information in the form when it is submitted .Here i am successfully able to send the mail when i'm sending static data. But on passing dynamic data from the form, i'm getting undefined error.
Cannot read property 'name' of undefined
app.js
var express = require('express');
var nodemailer = require("nodemailer");
var bodyParser = require('body-parser');
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail", // sets automatically host, port and connection security settings
auth: {
user: "[email protected]",
pass: "mypass"
}
});
http = require("http"),
fs = require('fs'),
cors = require('cors');
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// cross origin:
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
// end of cross
var config = require("./app/config.js")();
//Folder config.
app.use(express.static(__dirname + '/app/public/'));
app.use(express.static(__dirname + '/app/public/app/'));
app.get('/', function(req, res) {
res.sendfile('index.html');
});
app.post('/contact', function(req, res) {
console.log(req.body);
var data= req.body;
smtpTransport.sendMail({
from: "Sender Name <[email protected]>",
to: "Receiver Name <[email protected]>",
subject: "Confirmation Mail",
text: "Messege From "+data.name
}, function(error, response){ //callback
if(error){
console.log(error);
}else{
console.log(" Message sent");
}
smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
});
res.json(data);
});
http.createServer(app).listen(config.port, function() {
console.log("server listening on port " + config.port + " in " + config.mode + " mode");
console.log("http://localhost:" + config.port);
});
contact.js
'use strict';
angular.module('myApp')
.controller('ContactCtrl',['$scope', '$http', '$mdToast', '$animate',
function($scope, $http, $mdToast, $animate){
$scope.toastPosition = {
bottom: true,
top: false,
left: false,
right: true
};
$scope.getToastPosition = function() {
return Object.keys($scope.toastPosition)
.filter(function(pos) { return $scope.toastPosition[pos]; })
.join(' ');
};
this.sendMail = function() {
var data = ({
name :this.name,
email :this.email,
message :this.message
})
//Post Request
$http.post('/contact', data).
success(function(response, status, headers, config){
$mdToast.show(
$mdToast.simple()
.textContent('Thanks for your message '+data.name)
.position($scope.getToastPosition())
.hideDelay(50000)
);
}).
error(function(response, status, headers, config) {
$mdToast.show(
$mdToast.simple()
.textContent('Something went wrong, Please TRY AGAIN '+data.name)
.position($scope.getToastPosition())
.hideDelay(5000)
);
});
};
}
]);
Upvotes: 0
Views: 1722
Reputation: 2911
Aah, I think I found it. You are re initializing app
as var app = express();
at line 24. Hence whatever parsers you have set, are not actually getting used. Just remove that, and you should be able to get the data.
Upvotes: 1