javascript 如何修复[对象:空原型]{标题:'产品'}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56298481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to fix [Object: null prototype] { title: 'product' }
提问by Mohamed Aarab
I've started learning node.jswith expressframework , when I post a form like this :
我已经开始学习node.js用express的框架,当我张贴的形式是这样的:
router.get('/add-product',(req,res,next)=>{
res.send('<form action="/product" method="POST" ><input type="text" name="title" /><button type="submit">Submit</button></form>');
});
router.post('/product',(req,res,next)=>{
console.log(req.body);
res.redirect('/');
});
When I do console.log(req.body)it displays:
当我这样做时,console.log(req.body)它显示:
[Object: null prototype] { title: 'product' }
instead of just { title: 'product' }
而不仅仅是 { title: 'product' }
I'm wondering if this actually is an error with express or just a propriety that its been added to express recently , cause i downloaded another project created last year and it used the same approach, when i console.log(req.body)it display the same output.
我想知道这实际上是 express 的错误还是只是它最近添加到 express 的专有性,因为我下载了去年创建的另一个项目,它使用相同的方法,当我console.log(req.body)显示相同的输出时。
Thanks in advance for your help.
在此先感谢您的帮助。
回答by Jonas Wilms
Thats actually good design. Objects by default inherit the Object.prototypethat contains some helpers (.toString(), .valueOf()). Now if you use req.bodyand you pass no parameters to the HTTP request, then you'd expect req.bodyto be empty. If it would just be "a regular object" it wouldn't be:
这其实是很好的设计。默认情况下Object.prototype,对象继承包含一些帮助程序 ( .toString(), .valueOf()) 的 。现在,如果您使用req.body并且没有将参数传递给 HTTP 请求,那么您将期望req.body为空。如果它只是“一个普通对象”,它就不会是:
req.body.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.
有一种方法可以创建“空对象”,即没有任何属性/原型的对象,那就是Object.create(null). 您将在控制台中看到这些对象之一。
So no, this is not a bug that needs to be fixed, thats just great use of JS' features.
所以不,这不是一个需要修复的错误,这只是对 JS 特性的很好的利用。
回答by Tanzeel Saleem
Try this,
试试这个,
const obj = JSON.parse(JSON.stringify(req.body)); // req.body = [Object: null prototype] { title: 'product' }
console.log(obj); // { title: 'product' }
Happy Coding..!
快乐编码...!
回答by Onur Hangul
I get some problem and my terminal showed me below explanation
我遇到了一些问题,我的终端向我展示了以下解释
body-parser deprecated undefined extended: provide extended option at express
body-parser deprecated undefined extended: provide extended option at express
and i used this
app.use(bodyParser.urlencoded({extended: false}))
我用这个
app.use(bodyParser.urlencoded({extended: false}))
or
或者
you are running a version of Express that is 4.16+ then type just
您正在运行 4.16+ 的 Express 版本,然后只需键入
app.use(express.urlencoded({extended: true}))
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?
要了解有关扩展选项的更多信息,请阅读文档或此处有人回答得很好 - express 4.0 中的“扩展”是什么意思?
回答by Dmitry
This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.
这是 Express 中内置的中间件功能。它使用 JSON 有效负载解析传入请求,并基于正文解析器。
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option. This parser accepts any Unicode encoding of the body and supports automatic inflation of gzip and deflate encodings.
返回仅解析 JSON 并仅查看 Content-Type 标头与类型选项匹配的请求的中间件。此解析器接受正文的任何 Unicode 编码,并支持 gzip 和 deflate 编码的自动膨胀。
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred. app.use( express.urlencoded({ extended: true }) );
包含解析数据的新正文对象填充在中间件之后的请求对象上(即 req.body),或者如果没有正文要解析、Content-Type 不匹配,或者一个空对象 ({})发生了错误。app.use( express.urlencoded({extended: true }) );
回答by Martin Csomai
I had the same problem. I found out the problem was in my ejs file. I used this:
我有同样的问题。我发现问题出在我的 ejs 文件中。我用过这个:
<form method="POST">
But with ajax i set the method to post so somehow when i submitted the form it sent an empty Object and the values i gave. When i deleted the method from ejs the problem solved.
但是使用ajax,我将方法设置为以某种方式发布,当我提交表单时,它发送了一个空对象和我给出的值。当我从 ejs 中删除该方法时,问题解决了。
I hope it helps
我希望它有帮助
回答by Elton Marques
I recommmend to use the function JSON.parse like that:
我建议像这样使用 JSON.parse 函数:
const form = JSON.parse(JSON.stringify(req.body))
console.log(form.title)
const form = JSON.parse(JSON.stringify(req.body))
console.log(form.title)
回答by Sandeep Kumar
You can try this:
你可以试试这个:
app.use(express.urlencoded({ extended: false }));

