Node.js表示post参数总是未定义的

时间:2012-12-17 08:56:02

标签: javascript html forms node.js express

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

所以我使用Node.js和Express以及Jade视图引擎。

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

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

节点应用:

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'));
});

这是Home路线

/*
 * 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');
        }
    });

};

这是玉视图

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)

我希望能够获得'email'表单字段的值

我尝试了req.param('email'),req.body.email我已经尝试了标准的express.bodyParser()以及连接(有人在另一个答案中提到)一个需要('connect')。 bodyParser(),但我得到的所有内容都是未定义的。

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

我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

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