Node.js hasOwnProperty does not work even when the property exists
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": "sam@email.com",
"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?
javascript node.js hasownproperty
add a comment |
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": "sam@email.com",
"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?
javascript node.js hasownproperty
Please check adripofjavascript.com/blog/drips/… may help you
– IftekharDani
Nov 14 '18 at 5:42
can you please check demo: repl.it/@idanimob/AquaGracefulMicrostation its return true.
– IftekharDani
Nov 14 '18 at 5:53
add a comment |
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": "sam@email.com",
"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?
javascript node.js hasownproperty
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": "sam@email.com",
"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?
javascript node.js hasownproperty
javascript node.js hasownproperty
asked Nov 14 '18 at 4:59
codeinprogresscodeinprogress
53121127
53121127
Please check adripofjavascript.com/blog/drips/… may help you
– IftekharDani
Nov 14 '18 at 5:42
can you please check demo: repl.it/@idanimob/AquaGracefulMicrostation its return true.
– IftekharDani
Nov 14 '18 at 5:53
add a comment |
Please check adripofjavascript.com/blog/drips/… may help you
– IftekharDani
Nov 14 '18 at 5:42
can you please check demo: repl.it/@idanimob/AquaGracefulMicrostation its return true.
– IftekharDani
Nov 14 '18 at 5:53
Please check adripofjavascript.com/blog/drips/… may help you
– IftekharDani
Nov 14 '18 at 5:42
Please check adripofjavascript.com/blog/drips/… may help you
– IftekharDani
Nov 14 '18 at 5:42
can you please check demo: repl.it/@idanimob/AquaGracefulMicrostation its return true.
– IftekharDani
Nov 14 '18 at 5:53
can you please check demo: repl.it/@idanimob/AquaGracefulMicrostation its return true.
– IftekharDani
Nov 14 '18 at 5:53
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
I tested code and all checks returned true.
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293430%2fnode-js-hasownproperty-does-not-work-even-when-the-property-exists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
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')
);
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')
);
answered Nov 14 '18 at 5:04
CertainPerformanceCertainPerformance
83.2k144067
83.2k144067
add a comment |
add a comment |
I tested code and all checks returned true.
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
add a comment |
I tested code and all checks returned true.
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
add a comment |
I tested code and all checks returned true.
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
I tested code and all checks returned true.
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
}
var user = {
"uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad",
"first_name": "Sam",
"last_name": "Smith",
"email": "sam@email.com",
"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
}
answered Nov 14 '18 at 5:17
afshinafshin
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293430%2fnode-js-hasownproperty-does-not-work-even-when-the-property-exists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please check adripofjavascript.com/blog/drips/… may help you
– IftekharDani
Nov 14 '18 at 5:42
can you please check demo: repl.it/@idanimob/AquaGracefulMicrostation its return true.
– IftekharDani
Nov 14 '18 at 5:53