Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Object object has no method 'hasOwnProperty'

Tags:

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)

like image 749
UpTheCreek Avatar asked May 16 '13 10:05

UpTheCreek


People also ask

Has Own vs hasOwnProperty?

Both also support ES6 symbols. So what's the difference between the two? The key difference is that in will return true for inherited properties, whereas hasOwnProperty() will return false for inherited properties.

What does hasOwnProperty method do?

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

Can I use hasOwnProperty with an array?

The hasOwnProperty() method returns true if the property is directly present in the object (not in its prototype chain). If an object is an Array, then the hasOwnProperty() method can check if an index is available (not empty) in the array.

How do I check if an object has a property?

The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.


1 Answers

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.

like image 136
alex Avatar answered Oct 18 '22 19:10

alex