10

I have this problem, when I receive a request in the server the response is:

[Object: null prototype] { '{"someParameter": "potatoParameterValue", "someOtherParameter": "otherPotatoParameter}': '' }

I already used

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

and that doesn't work,someone else has had the same problem?

4
  • Hi Jonathan, would love some more information about your problem. How do you do your request? Postman or code to start with. Please try to improve your question with as much information and code you think is relevant. Commented Jan 31, 2020 at 14:57
  • Can you show us your query as well? Where does this result come from? Commented Jan 31, 2020 at 14:58
  • You can read the answer here: stackoverflow.com/questions/59994793/… Commented Jan 31, 2020 at 15:02
  • Does this answer your question? req.body is an empty object from simple HTML form using express Commented Jan 31, 2020 at 15:03

5 Answers 5

14

The only one reason why you get something like this: [Object: null prototype] it's because { extended: false };

Now, you try to change your extended to true. For an example, you can look at this code below:

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

I've tried that code above and it's working fine.

Sign up to request clarification or add additional context in comments.

Comments

1

Update Express

npm i express

The new version doesn't need body-parser But you can set extended to true

Comments

1

ok, first the request was coming from an app in nagular 8 which had an interceptor with a misconfigured header.

this was my header:

Content-Type': 'application/x-www-form-urlencoded;charset=utf-8

was replaced by this:

```Content-Type': 'application/json`

it is important to know this is the default header in case we do not assign a header.

then in the back, we started to receive an empty object, after that we just used

app.use(bodyParser.json());

and voilà, that was the solution to the problem.

Thank you all for your prompt response.

Comments

0

Let me try a fast ball, would love some more information..

It looks like you might be missing a " after otherPotatoParameter. Right now the key in the object is the object..

{ 
'{"someParameter": "potatoParameterValue", "someOtherParameter": "otherPotatoParameter}': '' 
}

Maybe it should be something like this?

{
  "someParameter": "potatoParameterValue", 
  "someOtherParameter": "otherPotatoParamete"
}

Comments

0

Use

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

or

app.use(express.urlencoded({extended: true}))

its working in my case

const express = require('express');

const app = express();

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

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

// app.use(express.urlencoded({extended: true}))

app.use('/',(req ,res ,next) => {

console.log('In the Another Middleware');
    // res.send('<title>Node Learning</title><h1>Hello From Express JS!</h1>');
    next();
})

app.use('/add-product',(req ,res , next) => {
   console.log('In the Another Middleware');
    res.send('<form method="POST" action="/product"><input type="text" name="title" /> 
 <button type="submin">Add</button></form>');
})
app.use('/product',(req , res , next) => {
   console.log(req.body);
    res.redirect('/');
});

app.use('/',(req ,res , next) => {
    console.log('In the Another Middleware');
    res.send('<title>Node Learning</title><h1>Hello From Express JS!</h1>');
})

app.listen(3000);

1 Comment

Welcome to SO! Please read the tour, and How do I write a good answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.