Tuesday, 4 April 2017

node.js req.body shows empty

I have tried all stuffs from other posts but I don't still get answers for it. I am creating a detail page for students and when I try it, req.body is always empty. Any ideas for me? Thank you for reading.

student.jade

extends layout

block content
    h1 Student list
    .studentlist
       table
            thead
                tr
                    th name
                    th age
            tbody               
                 each mongo_result, i in slist
                     form(action='/detail', method="POST") 
                        tr                                                   
                            td(type="text" name="name")= mongo_result.name
                            td(type="text" age="age")= mongo_result.age                             
                                button.btn-primary(type="submit" width='50' height='50') Detail

app.js

    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(express.methodOverride());
    app.use(require('stylus').middleware(path.join(__dirname, 'public')));
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(app.router);
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(express.bodyParser());

app.post('/detail', (req, res) => {    
//var data = res.json({ result: result });
console.log(req.body);
dbs.collection('slist').find({ name: req.body.name, age:1}).toArray((err, docs) => {
    if (err) return console.log(err)
    console.log(docs);
    res.render('detail', { title: 'Detail', year: new Date().getFullYear(), message: 'Detail of student', detail: docs});
}); 


})



via Chi Seong Oh

No comments:

Post a Comment