0

I get a JSON response back from firebase (cloud DB from google) with users data that i am saving in sessionStorage of the browser using JSON.stringify.

When i try to parse that data back out into a JSON object using JSON.parse so that i can reference the key/value pairs inside the JSON structure, i keep getting back text or a blank object in console.

I can stringify and parse it back and forth and log that, but i can't get it to parse into a "pretty JSON object" like i would normally get back from firebase. (where you click it in console and it will expand the tree of the JSON)

function renderLabsCharts(doc){
       var tmpDoc = sessionStorage.getItem('doc1');
       console.log('tempdoc ' + tmpDoc)
       var tmpJson = JSON.parse(tmpDoc);
       console.log('stringy temp doc'+JSON.stringify(tmpDoc))
       var ans = tmpJson[0];
       console.log('ans ' + ans)
       console.log('tmpjson ' + tmpJson)



        var labTime = tmpJson.labTestDate;
        console.log('labTime ' + labTime)
        labTime = JSON.stringify(labTime)
        console.log('labTime '+JSON.stringify(labTime))
         var labTimeDate = labTime.toDate();

Log Results

tempdoc {"labName":"LabCorp","labResult":"111","labTestDate":{"seconds":4100775060,"nanoseconds":0},"labTestName":"EST SENSITIVE (E2)","labUnits":"mL","stdRange":"a3333","uid":"WYqIp9f0dJR8F7OJrMAsk2if6as1"}
appLabs.js:499 stringy temp doc"{\"labName\":\"LabCorp\",\"labResult\":\"111\",\"labTestDate\":{\"seconds\":4100775060,\"nanoseconds\":0},\"labTestName\":\"EST SENSITIVE (E2)\",\"labUnits\":\"mL\",\"stdRange\":\"a3333\",\"uid\":\"WYqIp9f0dJR8F7OJrMAsk2if6as1\"}"
appLabs.js:501 ans undefined
appLabs.js:502 tmpjson [object Object]
appLabs.js:507 labTime [object Object]
appLabs.js:509 labTime "{\"seconds\":4100775060,\"nanoseconds\":0}"
appLabs.js:510 Uncaught TypeError: labTime.toDate is not a function
    at renderLabsCharts (appLabs.js:510)
    at HTMLFormElement.<anonymous> (appLabs.js:209)

When i try to access a key/value pair of the JSON object, i get error because it is not defined.

user3569450
  • 75
  • 10

2 Answers2

1

While the previous answer point out there is to toDate function in the object, I would like to share another thing about the console.log.

The reason you keep getting blank object in console is that you use + sign in console.log('string ' + object) function.

Javascript does the implicit type conversion when it saw a syntax like 'string' +, it's equal to the string concatenation for Javascript. Therefore it turns the object into String.

More : MDN Arithmetic operators Languages

You can print the actual object tree in this way console.log('some text:', object) instead.

function renderLabsCharts(doc){
       var tmpDoc = '{"labName":"LabCorp","labResult":"111","labTestDate":{"seconds":4100775060,"nanoseconds":0},"labTestName":"EST SENSITIVE (E2)","labUnits":"mL","stdRange":"a3333","uid":"WYqIp9f0dJR8F7OJrMAsk2if6as1"}';
       console.log('tempdoc ' + tmpDoc);
       var tmpJson = JSON.parse(tmpDoc);
    //   console.log('stringy temp doc'+JSON.stringify(tmpDoc))
       // var ans = tmpJson[0];
     //  console.log('ans ' + ans)
       console.log('tmpjson :' , tmpJson)



        var labTime = tmpJson.labTestDate;
        console.log('labTime :' , labTime)
       // labTime = JSON.stringify(labTime)
    //   console.log('labTime '+JSON.stringify(labTime))
         var labTimeDate = labTime.toDate();
 }
 
 renderLabsCharts();
AppleJam
  • 1,039
  • 8
  • 18
0

labTime is already in JavaScript object notation, you are calling stringify again, so labTime is a string not JSON, so obviously it is an error.

Corrected Code

    var labTime = tmpJson.labTestDate;
    var labTimeSeconds = labTime.seconds;

    //convert to date here
  • I had this at first, but it still results in ```appLabs.js:510 Uncaught TypeError: labTime.toDate is not a function``` Here is the Log output when i take stringify off ```labTime {"seconds":4100775060,"nanoseconds":0} ``` – user3569450 Aug 05 '19 at 10:16
  • There is no `toDate()` function in JavaScript – Narasimha Prasanna HN Aug 05 '19 at 10:19
  • Thanks, this must be a firebase thing that gives me back a workable javascript date format. I will look into proper way to convert this timestamp into js data in pure js. thanks! – user3569450 Aug 05 '19 at 10:22
  • Implement the corrections I told and then have a look at this : https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Narasimha Prasanna HN Aug 05 '19 at 10:23