Node.js hasOwnProperty does not work even when the property exists

1.6k Views Asked by At

In my Node.js Express application, when the user is logged in via passport the user user object is saved in the request.

It looks something like this:

{
    "uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
    "first_name": "Sam",
    "last_name": "Smith",
    "email": "[email protected]",
    "password": "$2a$10$fXYBeoK6s.A8xo2Yfgx4feTLRXpdvaCykZxr7hErKaZDAVeplk.WG",
    "profile_uuid": "db172902-f3c9-456d-8814-53d07d4ea954",
    "isActive": true,
    "deactivate": false,
    "verified": true,
    "ProviderUuid": "7149f8f1-0208-41db-a78e-887e7811a169"
}

But not every user has the ProviderUuid key present. So before using its value I am trying to check if the ProviderUuid key is present in the user object

var user = req.user;
console.log('---- provider: ' + JSON.stringify(user));
console.log('--- prop: ' + user.hasOwnProperty('ProviderUuid')); //returns false
console.log('---- other method prop check: ' + Object.prototype.hasOwnProperty.call(user, "ProviderUuid")); //returns false
if('ProviderUuid' in user){
    //this returns true
}

So doing user.hasOwnProperty('ProviderUuid') and Object.prototype.hasOwnProperty.call(user, "ProviderUuid")) returns false, however 'ProviderUuid' in user returns true.

What am I missing here?

2

There are 2 best solutions below

0
On BEST ANSWER

Since in works, the property must an inherited property, on one of user's prototype objects, rather than being on user itself. Here's a live example of such behavior:

const userProto = { foo: 'bar' };

// Create an empty object named `user` whose internal prototype is `userProto`:
const user = Object.create(userProto);

// False, user itself is an empty object, nothing's been assigned to it:
console.log(
  user.hasOwnProperty('foo')
);

// True, `foo` does exist on the *internal prototype* of the `user` object:
console.log(
  'foo' in user
);

// True, `foo` is a property directly on `userProto`:
console.log(
  userProto.hasOwnProperty('foo')
);

So, if you want to check for the existence of an inherited property named ProviderUuid, use the in operator like you're doing.

0
On

I tested code and all checks returned true.

var user = {
    "uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
    "first_name": "Sam",
    "last_name": "Smith",
    "email": "[email protected]",
    "password": "$2a$10$fXYBeoK6s.A8xo2Yfgx4feTLRXpdvaCykZxr7hErKaZDAVeplk.WG",
    "profile_uuid": "db172902-f3c9-456d-8814-53d07d4ea954",
    "isActive": true,
    "deactivate": false,
    "verified": true,
    "ProviderUuid": "7149f8f1-0208-41db-a78e-887e7811a169"
};
console.log('---- provider: ' + JSON.stringify(user));
console.log('--- prop: ' + user.hasOwnProperty('ProviderUuid')); //returns false
console.log('---- other method prop check: ' + Object.prototype.hasOwnProperty.call(user, "ProviderUuid")); //returns false
if('ProviderUuid' in user){
    //this returns true
}

Try JSON.parse(), Maybe req.user saved as string