最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node.js & express: req.body undefined - Stack Overflow

programmeradmin3浏览0评论

I'm currently designing a simple browser application utilizing express. I'm trying to extract the value a user selects in a drop down menu. I gave each option an individual value as well and have declared the method of the form as /post. but when I try which value they selected by going into the req.body, the value is undefined.

I recognize that the problem could lie with the body parser from browsing through similar questions (Example, Example1) but the solutions from these questions don't keep req.body from being undefined.

Here's my code for the app construction

const app = express()
app.use(express.static(__dirname, ''));
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname + '/public/views');
app.use(express.urlencoded());
app.set('view engine', 'html');
const server = http.createServer(app);

And here's the code for the post handling

app.get('/detailed', function(req,res){
    res.send(displayDetailed(results, req));
});
app.post('/detailed', function(req,res){
    res.send('Hello world');
    console.log(req.body);

});

When I post something in localhost:8080/detailed, the hello world returns just fine, but req.body is an empty (returns as {}). The displayDetailed function is a custom function that returns a html string with values extracted from a get request from the google sheets API. Since I'm not working with a saved html document, could this be affecting the process?

I'm currently designing a simple browser application utilizing express. I'm trying to extract the value a user selects in a drop down menu. I gave each option an individual value as well and have declared the method of the form as /post. but when I try which value they selected by going into the req.body, the value is undefined.

I recognize that the problem could lie with the body parser from browsing through similar questions (Example, Example1) but the solutions from these questions don't keep req.body from being undefined.

Here's my code for the app construction

const app = express()
app.use(express.static(__dirname, ''));
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname + '/public/views');
app.use(express.urlencoded());
app.set('view engine', 'html');
const server = http.createServer(app);

And here's the code for the post handling

app.get('/detailed', function(req,res){
    res.send(displayDetailed(results, req));
});
app.post('/detailed', function(req,res){
    res.send('Hello world');
    console.log(req.body);

});

When I post something in localhost:8080/detailed, the hello world returns just fine, but req.body is an empty (returns as {}). The displayDetailed function is a custom function that returns a html string with values extracted from a get request from the google sheets API. Since I'm not working with a saved html document, could this be affecting the process?

Share Improve this question asked May 28, 2018 at 15:39 dropTableUsersdropTableUsers 3651 gold badge4 silver badges14 bronze badges 6
  • how do you call /detailed? I mean, how do you "post something"? Also, you're not using bodyParser as far as I'm concerned – Lyubomir Commented May 28, 2018 at 15:42
  • can you please tell how are you posting data? – Yamini Chhabra Commented May 28, 2018 at 15:45
  • Maybe this will solve your problem: stackoverflow./questions/9177049/… – Dvoliq Commented May 28, 2018 at 15:47
  • @YaminiChhabra I'm trying to post the result of a form that consists various selects and a submit button which triggers the post. However, this is done within an asynchronous function. Could this cause the bodyparser to skip over the result? – dropTableUsers Commented May 28, 2018 at 23:16
  • @dropTableUsers No, this should not be the reason, post your async function please. – Yamini Chhabra Commented May 29, 2018 at 4:38
 |  Show 1 more ment

4 Answers 4

Reset to default 6

Most of the time req.body is undefined due to missing JSON parser

const express = require('express');
app.use(express.json());

could be missing for the body-parser

const bodyParser  = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

and sometimes it's undefined due to cros origin so add them

const cors = require('cors');
app.use(cors())

Did you setup the body parser for express to use? you can just npm install body-parser and then put these into your code.

const bodyParser = require('body-parser')
app.use(bodyParser.json())

Hope this can help!

When calling req.body outside of the async function (where the function constructing the html was called), req.body returned perfectly fine. I'll be modifying my project to account for this. I should have put this in the original question, but it did not seem relevant when I was writing the question. Thanks to everyone who responded

This will solve your issue:

App.use(bodyParser.urlencoded({extended: true}));
发布评论

评论列表(0)

  1. 暂无评论