Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getItem returns JSON_ERROR trying to get a value stored using objective c #140

Open
kperdomoc opened this issue Apr 29, 2019 · 0 comments
Open

Comments

@kperdomoc
Copy link

kperdomoc commented Apr 29, 2019

So Im rebuilding this iOS app with ionic and would like to migrate the old user data stored using NSUserDefaults. For example, the old app would store the username like this:

[defaults setObject:user.username forKey:@"username"];

And Im trying to get that back using getItem:

this.nativeStorage.getItem('username').then( data => { console.log("USERNAME: ", data) }, error => { console.log("ERROR: ", JSON.stringify(error)) }

This crashes and produces the following error:

code: 5 (JSON_ERROR)
source: JS
exception: {"line":400,"column":25,"sourceURL":"http://10.10.1.6:8100/plugins/cordova-plugin-nativestorage/www/mainHandle.js"}

Well, the exception tells the line number thats crashing. Opened the mainHandle file and turns out to be when trying to parse the result data from the device.

try { obj = JSON.parse(data); success(obj); } catch (err) { error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err)); }

I have debugged the plugin's ios code and it is indeed getting the data from the device storage. So the data exists and it's being returned fine. The problem comes when JS code tries to handle that using JSON.parse.

In my tests, lets say the stored value is username = '[email protected]', then the parsing will fail. But if i do setItem(username, "[email protected]"), then retrieve that, I get username = '"[email protected]"' (notice the double quotes included). So, seems that the plugin won't be able to retrieve values that are not JSON-parsable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant