gpt4 book ai didi

javascript - Node.js 表示 post 参数始终未定义

转载 作者:行者123 更新时间:2023-11-30 13:11:01 24 4
gpt4 key购买 nike

我是 Node.js 开发的新手,我知道已经有几个这样的堆栈溢出问题,不幸的是似乎没有一个能解决我的问题。所以我觉得我能做的就是问我的问题

所以我将 Node.js 与 Express 和 Jade View 引擎结合使用。

我的一些代码基于这篇文章:http://howtonode.org/express-mongodb

无论如何,这就是我所拥有的

Node 应用:

var express = require('express');
var home = require('./routes/home');
var d3demo = require('./routes/d3demo');
var PersonProvider = require('./public/javascripts/personProvider').PersonProvider;
var personProvider = new PersonProvider('localhost', 27017);
var LinkProvider = require('./public/javascripts/linkProvider').LinkProvider;
var linkProvider = new LinkProvider('localhost', 27017);


var http = require('http');
var path = require('path');
var app = express();

//=============================================================================
// EXPRESS SETUP
//=============================================================================
app.configure(function(){
app.set('port', process.env.PORT || 2000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
//app.use(require('connect').bodyParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function () {
app.use(express.errorHandler());
});


//=============================================================================
// ROUTING
//=============================================================================
app.get('/home', function (req, res) {
home.homeGet(req, res, commonHelper, personProvider, linkProvider);
});

app.post('/home', function (req, res) {
home.homePost(req, res, personProvider);
});


var server = http.createServer(app);


server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});

这是回家的路

/*
* GET home page.
*/

exports.homeGet = function(req, res, commonHelper, personProvider, linkProvider){
commonHelper.seedData(personProvider, linkProvider, function() {
res.render('home');
});
};

exports.homePost = function (req, res, personProvider) {

var newUserEmail = req.body.email;
console.log(req.body.length);


//console.log(x);

//var email = req.param('Email');
console.log("/Home posted Email :" + newUserEmail);

personProvider.save({
//email: req.param('Email'),
email: newUserEmail,
}, function (error, docs) {
if(error == null) {
res.redirect('/d3demo');
} else {
res.render('home');
}
});

};

这就是 Jade 观

extends layout

block head
link(rel='stylesheet', href='/stylesheets/home.css')
script(src='/javascripts/home.js')

block content
form(method='post', id='homeForm', action='http://localhost:2000/home')
div(id='dialog', title='error', style='display:none;')
p You need to supply a valid email
div(id='NewDetailsArea')
p Enter your email address, and then click enter
| <input type="text" id="email" class="email"></input>
div#homeSubmit
input(type='submit', value='Enter', id='enterEmail')

渲染到这里

<form method="post" id="homeForm" action="http://localhost:2000/home">
<div id="dialog" title="error" style="display:none;">
<p>You need to supply a valid email</p></div>
<div id="NewDetailsArea">
<p>Enter your email address, and then click enter </p>
<input type="text" id="email" class="email">
</input><div id="homeSubmit"><input type="submit" value="Enter" id="enterEmail">
</div>
</div>
</form>

所以问题:

其实问题很简单。函数内

homePost = function (req, res, personProvider)

我希望能够获取“电子邮件”表单字段的值

我已经尝试过 req.param('email'), req.body.email 我已经尝试了标准的 express.bodyParser() 和连接(有人在另一个答案中提到过)一个 require('connect')。 bodyParser(),但可惜我得到的是未定义的。

此外,如果我尝试使用 console.log(req.body),我会得到未定义

我做错了什么?

最佳答案

您需要为电子邮件输入提供一个 name 属性。 name 是提交表单时发送的内容:

<input type="text" id="email" name="email" class="email">

关于javascript - Node.js 表示 post 参数始终未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13910991/

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