简体   繁体   中英

Jade and Express.js - req.body.[name of form] is undefined

I'm trying to update a database using a drop down form, but req.body.[name of form] is undefined.

req.body in console shows {}.

Here is my lev.js:

router.post('/kundeendring', function(req, res) {
    var test1 = req.body.ansvarlig;
    console.log(test1);

And here is the form in my lev.jade:

form#kunde(method="post", action="kundeendring")
    select (name= "ansvarlig")
        option(value='Kim', selected=kim) Kim 
        option(value='Christer', selected=christer) Christer
        option(value='Hege', selected=hege) Hege
        option(value='Morten', selected=morten) Morten
        option(value='Andre', selected=andre) Andre
    h3= "Lagre"
    button#btnSubmit(type="submit") submit

I also have bodyParser automatically installed using the express generator:

var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

When I click the submit button test1 is 'undefined'.

edit: Fixed a missing parentheses.

edit: This works in lev.jade, but still not able to transfer that to drop down:

form#formAddUser(name="adduser",method="post",action="kundeendring")
    input#inputUserName(type="text", placeholder="username", name="username")
    input#inputUserEmail(type="text", placeholder="useremail", name="useremail")
    button#btnSubmit(type="submit") submit

It was a simple and stupid white space error.

The select tag didn't get a name.

Changing to select(name= "ansvarlig") solved everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM