Shipra
Shipra

Reputation: 1299

Node.js : check if a property is absent from object

I’m checking if a property 'lessons' (not inherited) is not present in obj, All these give me true

(typeof obj['lessons'] == undefined)

(!(obj.hasOwnProperty('lessons')))

(!(hasOwnProperty.call(obj,'lessons')))

(!(_.has(obj, 'lessons')))

(!Object.prototype.hasOwnProperty.call(obj, 'lessons’))

but the property is present in the object, when i print keys by using (key in obj). I don't want to use it as it's very slow and my object is huge.

I found this on stackoverflow, but I don't understand what it's trying to do or how to use it with node.js.

I'd also like to know how are the mentioned ways of using hasOwnProperty are different.

EDIT adding code

My code is:

console.log("LS:",JSON.stringify(obj)) ;
if (typeof obj['lessons'] == undefined)
    console.log('typeof undefined');
else {console.log('typeof not undefined');}

if (!(obj.hasOwnProperty('lessons')))
    console.log('hasOwnProperty: false');
else {console.log('hasOwnProperty not undefined');}

if (!(hasOwnProperty.call(obj,'lessons')))
    console.log('hasOwnProperty.call');
else {console.log('hasOwnProperty.call not undefined');}

if (!(_.has(obj, 'lessons')))
    console.log('_.hash');
else {console.log('_has not undefined');}

if (!(_.has(obj, 'lessons')))
    {obj['lessons'] = {"levels": []};}
else
    {console.log("has lesson level ");}

console.log("Lesson ", JSON.stringify(obj.lessons));

And the output I'm getting is:

LS: {"_id":"N9zmznzAWx","time":"1970-01-01T00:33:36.000Z","lessons":{"levels":["abc"]}}
typeof not undefined
hasOwnProperty: false
hasOwnProperty.call
_.hash
Lesson {"levels":[]}

Same case with all others..

SOLUTION It works when I use JSON.parse(JSON.stringify(obj)) instead of obj.

Upvotes: 0

Views: 1943

Answers (3)

JohnnyHK
JohnnyHK

Reputation: 311895

Your checks aren't working because Mongoose document objects don't use simple object properties to expose their fields.

Instead, you can use the Document#get method to do this (with your obj renamed to doc):

var isMissing = (doc.get('lessons') === undefined);

Or you can create a plain JS object from your document by calling toObject() on it, and then use hasOwnProperty on that:

var obj = doc.toObject();
var isMissing = !obj.hasOwnProperty('lessons');

Upvotes: 1

King Friday
King Friday

Reputation: 26076

typeof returns a string

var obj = {};
console.log(typeof (typeof obj.lessons));

https://jsfiddle.net/0ar2ku7v/

So you must compare it as such:

if (typeof obj.lessons === 'undefined')
  console.log('nope');

Upvotes: 0

omarjmh
omarjmh

Reputation: 13886

Here is a function that I wrote as an example to test three ways you have listed in your question:

function check(obj) {
  if (!obj.hasOwnProperty("lessons")) {
    console.log('nope');
  }  
  if (!_.has(obj, 'lessons')) {
    console.log('nope');
  }  
  if (!obj.lessons) {
    console.log('nope');
  }
}

Here is JSBIN that runs the function on two objects, one with 'lessons' and one without:

Example JsBIN

Upvotes: 0

Related Questions