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












0















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?










share|improve this question























  • 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
















0















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?










share|improve this question























  • 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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















4














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.






share|improve this answer































    0














    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






    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      4














      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.






      share|improve this answer




























        4














        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.






        share|improve this answer


























          4












          4








          4







          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.






          share|improve this 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.






          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')
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 5:04









          CertainPerformanceCertainPerformance

          83.2k144067




          83.2k144067

























              0














              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






              share|improve this answer




























                0














                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






                share|improve this answer


























                  0












                  0








                  0







                  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






                  share|improve this answer













                  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
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 5:17









                  afshinafshin

                  11




                  11






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python