javascript Node.js 对象对象没有方法“hasOwnProperty”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16585209/
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
Node.js Object object has no method 'hasOwnProperty'
提问by UpTheCreek
Not sure why hasOwnProperty()
seems to be missing from my object...
不知道为什么hasOwnProperty()
我的对象似乎不见了......
I'm getting data from an http post in expressjs3, like this:
我正在从 expressjs3 中的 http 帖子获取数据,如下所示:
someControllerFunction: function(req, res){
var data = req.body.loc;
...
}
However if I do:
但是,如果我这样做:
data.hasOwnProperty('test');
I get:
我得到:
Object object has no method 'hasOwnProperty'
Perhaps I'm missing something obvious, but what?
也许我错过了一些明显的东西,但是什么?
(Node 10.5, Express 3.2.1)
(节点 10.5,Express 3.2.1)
回答by alex
The object may not have Object.prototype
as its prototype.
该对象可能没有Object.prototype
作为其原型。
This is the case if the object was created with...
如果对象是用...创建的,就是这种情况。
var data = Object.create(null);
You could use...
你可以用...
Object.prototype.hasOwnProperty.call(data, 'test');
...to test if the property exists.
...测试属性是否存在。
回答by Vasily
This hack works for me:
这个 hack 对我有用:
req.body = JSON.parse(JSON.stringify(req.body));