Can't access keys in a json that is returned from mongoose fineOne() [duplicate]












0
















This question already has an answer here:




  • Can't access object property of a Mongoose response

    2 answers




I am encountering a weird issue.
I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:



Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
res.send(existingDoc );
});


Now, everything works until now, it sends the json I expected to get. The looks like so:



{
"_id": "5bf388cf170a974770c5c942",
"ID": "11/2018",
"date": "2018-11-20T04:08:47.997Z",
"total": {
"total_market_cap": [
64301.06256298704
]
}
}


The problem is that when I try to access these values for example:



 console.log(existingDoc.total);


I get undefined. Tried also using:



 console.log(existingDoc['total']);


And I still get undefined.



It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.










share|improve this question















marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Can't access object property of a Mongoose response

      2 answers




    I am encountering a weird issue.
    I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:



    Model.findOne({
    ID: ID
    }).then(existingDoc => {
    console.log(existingDoc );
    res.send(existingDoc );
    });


    Now, everything works until now, it sends the json I expected to get. The looks like so:



    {
    "_id": "5bf388cf170a974770c5c942",
    "ID": "11/2018",
    "date": "2018-11-20T04:08:47.997Z",
    "total": {
    "total_market_cap": [
    64301.06256298704
    ]
    }
    }


    The problem is that when I try to access these values for example:



     console.log(existingDoc.total);


    I get undefined. Tried also using:



     console.log(existingDoc['total']);


    And I still get undefined.



    It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.










    share|improve this question















    marked as duplicate by Neil Lunn javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 20 '18 at 20:18


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Can't access object property of a Mongoose response

        2 answers




      I am encountering a weird issue.
      I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:



      Model.findOne({
      ID: ID
      }).then(existingDoc => {
      console.log(existingDoc );
      res.send(existingDoc );
      });


      Now, everything works until now, it sends the json I expected to get. The looks like so:



      {
      "_id": "5bf388cf170a974770c5c942",
      "ID": "11/2018",
      "date": "2018-11-20T04:08:47.997Z",
      "total": {
      "total_market_cap": [
      64301.06256298704
      ]
      }
      }


      The problem is that when I try to access these values for example:



       console.log(existingDoc.total);


      I get undefined. Tried also using:



       console.log(existingDoc['total']);


      And I still get undefined.



      It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.










      share|improve this question

















      This question already has an answer here:




      • Can't access object property of a Mongoose response

        2 answers




      I am encountering a weird issue.
      I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:



      Model.findOne({
      ID: ID
      }).then(existingDoc => {
      console.log(existingDoc );
      res.send(existingDoc );
      });


      Now, everything works until now, it sends the json I expected to get. The looks like so:



      {
      "_id": "5bf388cf170a974770c5c942",
      "ID": "11/2018",
      "date": "2018-11-20T04:08:47.997Z",
      "total": {
      "total_market_cap": [
      64301.06256298704
      ]
      }
      }


      The problem is that when I try to access these values for example:



       console.log(existingDoc.total);


      I get undefined. Tried also using:



       console.log(existingDoc['total']);


      And I still get undefined.



      It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.





      This question already has an answer here:




      • Can't access object property of a Mongoose response

        2 answers








      javascript node.js json mongodb mongoose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 22:39







      Randomizer

















      asked Nov 20 '18 at 18:33









      RandomizerRandomizer

      10010




      10010




      marked as duplicate by Neil Lunn javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 '18 at 20:18


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Neil Lunn javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 '18 at 20:18


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          2














          Can you try to convert it toObject






          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });








          share|improve this answer
























          • It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

            – Randomizer
            Nov 20 '18 at 20:00











          • Could you add more explanation about this behavior? it looks interesting.

            – Samuel
            Nov 20 '18 at 20:13











          • Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

            – shmit
            Nov 20 '18 at 20:16


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Can you try to convert it toObject






          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });








          share|improve this answer
























          • It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

            – Randomizer
            Nov 20 '18 at 20:00











          • Could you add more explanation about this behavior? it looks interesting.

            – Samuel
            Nov 20 '18 at 20:13











          • Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

            – shmit
            Nov 20 '18 at 20:16
















          2














          Can you try to convert it toObject






          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });








          share|improve this answer
























          • It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

            – Randomizer
            Nov 20 '18 at 20:00











          • Could you add more explanation about this behavior? it looks interesting.

            – Samuel
            Nov 20 '18 at 20:13











          • Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

            – shmit
            Nov 20 '18 at 20:16














          2












          2








          2







          Can you try to convert it toObject






          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });








          share|improve this answer













          Can you try to convert it toObject






          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });








          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });





          Model.findOne({
          ID: ID
          }).then(existingDoc => {
          console.log(existingDoc );
          let newdoc = existingDoc.toObject();
          console.log(newdoc.myProperty)
          res.send(existingDoc );
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 19:57









          shmitshmit

          507310




          507310













          • It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

            – Randomizer
            Nov 20 '18 at 20:00











          • Could you add more explanation about this behavior? it looks interesting.

            – Samuel
            Nov 20 '18 at 20:13











          • Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

            – shmit
            Nov 20 '18 at 20:16



















          • It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

            – Randomizer
            Nov 20 '18 at 20:00











          • Could you add more explanation about this behavior? it looks interesting.

            – Samuel
            Nov 20 '18 at 20:13











          • Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

            – shmit
            Nov 20 '18 at 20:16

















          It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

          – Randomizer
          Nov 20 '18 at 20:00





          It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!

          – Randomizer
          Nov 20 '18 at 20:00













          Could you add more explanation about this behavior? it looks interesting.

          – Samuel
          Nov 20 '18 at 20:13





          Could you add more explanation about this behavior? it looks interesting.

          – Samuel
          Nov 20 '18 at 20:13













          Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

          – shmit
          Nov 20 '18 at 20:16





          Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject

          – shmit
          Nov 20 '18 at 20:16



          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory