Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fitbit OS Simulator: 'Unexpected token'

ANSWERED

When I try to run my app via the simulator, I get this:

[5:10:39 PM]SyntaxError: Unexpected token o in JSON at position 1
[5:10:39 PM]Uncaught SyntaxError: Unexpected token o in JSON at position 1

No file or line number is reported, which makes debugging a bit hard. I guess the problem is with some internal code (although I'm probably causing it). I've tried using 'console.log' to see where execution gets to, but it seems to get nowhere.

 

When I run the app on real devices, it's fine.

 

Does anyone have any tips about how to debug this?

Peter McLennan
Gondwana Software
Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

When you say real hardware, are you testing on iOS or Android?

 

 

The issue is this: on line 51 you call this: settingsStorage.setItem('accountStatus',{'name':accountStatusMsg});

 

The second argument to setItem is expected to be a string, but you pass an object. What happens is that the object is changed into a string, and .toString() on an object is [object Object]. Your settings then tries to parse this as valid JSON and clearly, it isn't.

 

You can fix this by calling JSON.stringify before passing to setItem, but I'm curious if iOS or Android are differing in behaviour here for you.

View best answer in original post

Best Answer
8 REPLIES 8

After a long process of trial-and-error, I've isolated the problem down to this line in companion/index.js:

settingsStorage.setItem('accountStatus',{'name':'abc'});

...where the corresponding component is declared in settings/index.jsx like this:

<TextInput
          label="Status"
          placeholder="Not created."
          settingsKey="accountStatus"
          disabled="true"
        />

It works fine on real hardware, but not in the simulator 0.5.0. Perhaps a bug to investigate...

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Could you private message me an export of your project from Studio? We'll try to reproduce this issue and see what's going on. Thanks!

Best Answer
Thanks for the offer! It's on the way.
Peter McLennan
Gondwana Software
Best Answer
0 Votes

This problem still occurs with simulator 0.5.2.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

When you say real hardware, are you testing on iOS or Android?

 

 

The issue is this: on line 51 you call this: settingsStorage.setItem('accountStatus',{'name':accountStatusMsg});

 

The second argument to setItem is expected to be a string, but you pass an object. What happens is that the object is changed into a string, and .toString() on an object is [object Object]. Your settings then tries to parse this as valid JSON and clearly, it isn't.

 

You can fix this by calling JSON.stringify before passing to setItem, but I'm curious if iOS or Android are differing in behaviour here for you.

Best Answer

Many thanks for the reply and explanation. The code works fine on Android hardware, so there's a difference in behaviour.

 

If the offending line is changed to the string 'abc' rather than an object, it still crashes the simulator from memory. But I'll check this later today, and investigate whether data type conversions help.

 

The SDK complains about the use of life-cycle code before it dies; I wouldn't have thought that this should cause the observed problem so I think it's a red herring, but I could be wrong.

 

Ta again!

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Liam, you were, of course, exactly right. JSON.stringify(...) fixed it.

 

FWIW, the companion on Android was perfectly happy without it. This is a bit surprising because I would have assumed that the code base was similar.

 

Many thanks for looking at this. Development is much faster and more flexible using the simulator. 🙂

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Glad to hear that worked! I'll raise the issue internally about inconsistency across platforms, thanks! 🙂

Best Answer
0 Votes