gpt4 book ai didi

node.js - 如何摆脱 [Object : null prototype] in Node. js?

转载 作者:行者123 更新时间:2023-12-04 22:39:33 25 4
gpt4 key购买 nike

有没有办法摆脱 [Object: null prototype]在终端中,所以它只会显示 {title: 'book'} ?

我在做 console.log(req.body);在 Node/express.js

terminal

最佳答案

这个额外的 [对象:空原型(prototype)] 当我们控制台记录一些具有 的对象时,Node 中会出现问题空 原型(prototype)...

这只是意味着该对象不会有它的内置方法...例如 => .toString() 或 .hasOwnProperty() 等...

const obj1 = Object.create(null);
obj1['key'] = 'SomeValue' ;
console.log(obj1);
>> [Object: null prototype] { 'key' : 'SomeValue' }

const obj2 = {};
obj2['key'] = 'SomeValue' ;
console.log(obj2);
>> { 'key' : 'SomeValue' }

当我们在 中设置扩展时app.use(urlencoded{ ... }) 的选项真实 => URL 编码数据由 解析qs 图书馆 ,

当我们将其设置为 false 时,它​​会被 解析查询字符串 图书馆...

在查询字符串库 ( https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options )

它明确指出

返回的对象查询字符串.parse() 方法在原型(prototype)上并不继承自 JavaScript 对象。这意味着 obj.toString()、obj.hasOwnProperty() 等典型的Object 方法没有定义并且不会起作用。

或者换句话说,他们有 空原型(prototype) ...

这就是为什么在 {extended:false} 的情况下,当我们 console.log(req.body) => 输出包含额外的 [对象:空原型(prototype)] 在一开始的时候...

对于其他差异,请使用

what the difference between qs and querystring

关于node.js - 如何摆脱 [Object : null prototype] in Node. js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53636028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com