UpTheCreek UpTheCreek - 4 years ago 216
Javascript Question

Node.js Object object has no method 'hasOwnProperty'

Not sure why

hasOwnProperty()
seems to be missing from my object...

I'm getting data from an http post in expressjs3, like this:

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)

Answer Source

The object may not have Object.prototype as its 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download