how to reproduce this issue?
- create a project with express-generator
- in
routes/index.js
add the below line:
router.post('/', function(req, res, next) {
console.log(req.body.hasOwnProperty('asdf'));
});
- run the server
- use postman to send a POST request with a couple of body parameters.
- you’ll get:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<h1>req.body.hasOwnProperty is not a function</h1>
<h2></h2>
<pre></pre>
</body>
</html>
req.body.hasOwnProperty is not a function
Why this happening? is this behaviour expected?
Well, assuming that the above was the only change, then the answer would be that
req.body
is the result of Node.js corequerystring
module’sparse
function (https://nodejs.org/dist/latest-v6.x/docs/api/querystring.html#querystring_querystring_parse_str_sep_eq_options), which doesn’t inherit fromObject
:@wesleytodd Ahh! missed it😅
So this is the expected behavior. (it would be awesome if it wasn’t. lol )
Now I can
to fix the issue.