无法读取未定义的属性“用户名” - 通过电子邮件在 Node.js 中发送表单数据
Cannot read property 'username' of undefined - Sending form data via email in Node.js
我在填写联系表格并按下提交按钮时尝试创建一个简单的网页,它应该发送电子邮件。
我正在尝试为此使用 Node.js,但出现以下错误:
这就是我到目前为止所做的。
无法读取未定义的属性"用户名"
应用.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.post('/contact', function(req,res){
var mailOpts, smtpTrans;
smtpTransport = nodemailer.createTransport("SMTP",{
host: "smtp.gmail.com",
secureConnection: true,
port: 465,
auth: {
user: "myemail@gmail.com",
pass: "secret"
}
});
mailOpts = {
from: req.body.username + ' <' + req.body.email + '>',
to: 'qgerard.gerard@gmail.com',
subject: 'Website contact',
text: req.body.message
};
smtpTrans.sendMail(mailOpts, function(error, info){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
console.log(req.body.username + ' <' + req.body.email + '>');
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
索引.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/contact', function(req, res, next){
res.render('contact', {title: 'Cantact'})
});
module.exports = router;
联系人.翡翠
extends layout
block content
h1= title
p Welcome to #{title}
div.content
form(action="/contact", name="contactus", id="contactus", method="post")
fieldset
p
label(for="username") Name:
br
input(name="username", type="text", id="username")
p
label(for="email") Email:
br
input(name="email", type="email",id="email")
p
label(for="message") Message:
br
textarea(name="message", cols="40", rows="10", id="message")
p
input(name="submit", type="submit", value="Send", id="submit")
我不知道我在这里错过了什么。如果有人知道如何解决此问题,请告诉我我需要什么依赖项(如果需要)以及我需要在哪里修复我的错误。
亲切问候
您需要
在定义路由之前设置body-parser
。
将您的快速应用程序配置移到app.post('/contact')
路由上方:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.post('/contact', function(req,res){
//....
});
在注册post
之后注册bodyparser
。将app.use
行移到app.post
之前,我认为它会起作用。
相关文章:
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色
- 使用angularjs验证文本框中的电子邮件
- 无法读取未定义的属性“用户名” - 通过电子邮件在 Node.js 中发送表单数据
- 从 Node.JS使用 MailGun 发送 HTML 电子邮件
- 使用官方 Node.js SendGrid API 发送电子邮件时,响应 JSON 中可能出现哪些错误消息
- 使用Heroku上的node mailer发送延迟的电子邮件
- Node.js mongodb电子邮件验证
- 使用 sendgrid 发送电子邮件 [Node.js]
- Node.js中的Mailgun电子邮件验证API
- 如何在node.js上使用邮戳电子邮件
- Node.js:循环,从查询中发送每个匹配行的电子邮件,但如果它通过#匹配数发送电子邮件给同一用户
- 我如何从我的Gmail帐户在我的Node.js脚本发送电子邮件
- 我如何使用Node.js发送电子邮件每10秒
- 发送电子邮件与图像附件在node.js
- 我如何发送电子邮件的每一个错误,发生在Node.js
- 在imap Node.js中搜索电子邮件
- Node Webkit-通过outlook打开和发送电子邮件
- Node.js:从字符串中读取电子邮件的正则表达式
- Node.js:用于获取电子邮件头和正文的正则表达式
- Node.js:我如何访问使用node-imap接收的电子邮件的“To”,“Body”和“From”字段?