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

时间:2021-01-09 11:09:44

I am pretty new to Node.js development, and I am aware that there are several stack overflow questions like this already, unfortunately none seem to fix my problem. So I feel all I can do is ask my question

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

So I am use Node.js with Express and the Jade view engine.

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

I based some of my code on this article : http://howtonode.org/express-mongodb

我在本文中提供了一些代码:http://howtonode.org/express-mongodb

Anyway here is what I have

无论如何这里是我的

The node app :

节点应用:

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

and this is the Home route

这是家庭路线

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

};

And this is the jade view

这是玉的观点

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

Which gets rendered to this

哪个被渲染到这个

<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>

So the problem:

所以问题:

Well the problem is actually pretty simply. Within the function

那问题其实很简单。在功能内

homePost = function (req, res, personProvider)

homePost = function(req,res,personProvider)

I would like to be able to get the value of the 'email' form field

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

I have tried req.param('email'), req.body.email I have tried the standard express.bodyParser() and also the connect (which someone mentioned in another answer) one require('connect').bodyParser(), but alas all I get is undefined.

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

Also if I try and console.log(req.body) I get undefined

此外,如果我尝试和console.log(req.body)我得到undefined

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


3  

You need to supply a name attribute for the email input. The name is what gets sent when the form is submitted:

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

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

#1


3  

You need to supply a name attribute for the email input. The name is what gets sent when the form is submitted:

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

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