#How to map from JSON. Getting a JSON.parse Error

29 messages · Page 1 of 1 (latest)

thorny beacon
#

The error:
SyntaxError: Unexpected token o in JSON at position 1at JSON.parse (<anonymous>)

Code

export const createReview = functions.region("europe-west2")
    .firestore.document("timesheets/{timesheetId}")
    .onUpdate(async (change) => {
      const timesheet = change.after.data();
      if (timesheet) {
        let parsedDaysWorked: { [key: string]: { time: number;
          miles: number; expenses: number; } } = {};

        parsedDaysWorked = JSON.parse(timesheet.days_worked);
      }
    });

The picture show the firestore doc

wanton wadi
#

try logging timesheet.days_worked to see what it is to maybe see what you messed up

thorny beacon
#

This is what i get from logs

timesheet.days_worked: [object Object]

Code:
``` console.log(timesheet.days_worked: ${timesheet.days_worked});

hushed mortar
#

log it without stringifying

thorny beacon
#

how do i that?

#

The code:

export const createReview = functions.region("europe-west2")
    .firestore.document("timesheets/{timesheetId}")
    .onUpdate(async (change) => {
      const timesheet = change.after.data();
      if (timesheet) {
        console.log(`timesheet.days_worked: ${timesheet.days_worked}`);
      }
    });
wanton wadi
#

don't put it in a string

thorny beacon
#

yh k

wanton wadi
#
- console.log(`text ${obj}`);
+ console.log("text", obj);
thorny beacon
#

i was just being dumb ty

wanton wadi
#

anyways from that log

#

it's not a string, don't parse it

#

it's already a parsed object

thorny beacon
#

Only a sample of the logs as the whole things is too long its printing out the value of daysWorked

 daysWorked {
 '1': [
 {
client_doc_id: null,
client_name: 'Crocky Trail',
...
 miles: 5,
 project_doc_id: 'xDqvg58K6qxu4M5q7mvZ',
 project_id: 7211
 },
 {
client_doc_id: null,
 client_name: null,
...
 project_doc_id: 'MbgcJ6bz4LJTdcowyGC1',
project_id: 5537
 }
],
'2': [
wanton wadi
#

doesn't change my answer

#

just the [object Object] solidifies it tbh

#

which ig could have been guessed from the token 'o' at position 1 but didn't want to guess that at first

thorny beacon
#

so if its a prsed obj do i use something like timesheet.days_worked.get("1")?

wanton wadi
#

no, just days_worked[1] just like any other js object

#

why is days_worked an object instead of an array though

thorny beacon
#

becuase im guessing i have it as a map in firestore where i store the docments: {"1":day, "5" }. The reason for making it map was becuase i dont know the order in which the user will add the objects. So i made it map. For example the user can plan for tuesdsay ("2") and friday ("5") on a monday. And then tuesday they might end up remembering they forgot to add in for monday. I figured a map would be the easiest given the insane scenario

wanton wadi
#

no that's just an object

#

but ok. why not just use weekday names then

thorny beacon
#

only becuase numbers numbers shorter, which ik isnt gd but trying to keep everything in same doc if i do get tight for space might have end up changing the strucutre

blazing jay
#

Make sure you try/catch the json.parse, it will throw if it cant parse

wanton wadi
#

readability is king

wanton wadi
#

either it will always parse or it will never parse, with this kind of thing