how to fix [Object: null prototype] { title: 'product' }

That’s actually good design. Objects by default inherit the Object.prototype that contains some helper functions (.toString(), .valueOf()). Now if you use req.body and you pass no parameters to the HTTP request, then you'd expect req.body to be empty. If it were just "a regular object", it wouldn't be entirely empty:

console.info(({ "toString": 5 })['toString']);   // 5
console.info(({})['toString']);                  // [Function: toString]

There is a way to create "empty objects", meaning objects without any properties / prototype, and that is Object.create(null). You are seeing one of those objects in the console.

So no, this is not a bug that needs to be fixed, that’s just great use of JS' features.


I get some problem and my terminal showed me below explanation

body-parser deprecated undefined extended: provide extended option at express

and i used this app.use(bodyParser.urlencoded({extended: false}))

or

you are running a version of Express that is 4.16+ then type just

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

I think it helps you

To find out more about the extended option, read the docs or someone here has answered it well - What does 'extended' mean in express 4.0?


Try this,

const obj = JSON.parse(JSON.stringify(req.body)); // req.body = [Object: null prototype] { title: 'product' }

console.log(obj); // { title: 'product' }

Happy Coding..!