03-13-2018 23:20
03-13-2018 23:20
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?
Answered! Go to the Best Answer.
03-26-2018 02:43
03-26-2018 02:43
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.
03-14-2018 20:15
03-14-2018 20:15
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...
03-15-2018 09:16
03-15-2018 09:16
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!
03-16-2018 14:44
03-16-2018 14:44
03-25-2018 19:30
03-25-2018 19:30
This problem still occurs with simulator 0.5.2.
03-26-2018 02:43
03-26-2018 02:43
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.
03-26-2018 12:54
03-26-2018 12:54
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!
03-26-2018 20:52
03-26-2018 20:52
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. 🙂
03-27-2018 03:11
03-27-2018 03:11
Glad to hear that worked! I'll raise the issue internally about inconsistency across platforms, thanks! 🙂