gpt4 book ai didi

javascript - 无法读取未定义的属性 'username' - 在 Node.js 中通过电子邮件发送表单数据

转载 作者:太空宇宙 更新时间:2023-11-04 03:07:06 25 4
gpt4 key购买 nike

我正在尝试在填写联系表单并按提交按钮时创建一个简单的网页,它应该发送一封电子邮件。

我尝试使用 Node.js 来实现此目的,但收到以下错误:

这就是我到目前为止所做的。

Cannot read property 'username' of undefined

app.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;

index.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;

contact.jade

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

将您的 Express 应用配置移至 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){
//....
});

关于javascript - 无法读取未定义的属性 'username' - 在 Node.js 中通过电子邮件发送表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36860464/

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