TheCocoaProject / cordova-plugin-nativestorage

Cordova plugin: Native storage of variables in Android, iOS and Windows
http://thecocoaproject.github.io/
Apache License 2.0
292 stars 106 forks source link

JSON_ERROR #35

Closed PierBover closed 8 years ago

PierBover commented 8 years ago

Hi

I'm having a weird error when retrieving some data with NativeStorage.getItem().

The data is retrieved ok at first instance, but afterwards some millisenconds later the error callback is called with an error code 5.

When I log the data in Android it seems the json is cut, logcat doesn't show the whole JSON.

The data is less than 100KB...

Any idea what might be happening?

GillesC commented 8 years ago

So you first get the item correct, without any errors. Thus from the success-callback? And afterwards the error-callback is being invoked?

PierBover commented 8 years ago

Yes

PierBover commented 8 years ago

Is it possible the callbacks are triggered if I'm doing NativeStorage.setItem(...) somewhere else on my code?

GillesC commented 8 years ago

As you can see here in the code, the object is first parsed and if there isn't any exceptions the object is giving back via the success-callback.

Could you check the following:

Please post these values so I could help you debugging.

GillesC commented 8 years ago

@PierBover No that shouldn't be any problem. Unless you use the same success/error callback functions. Otherwise the callbacks from another getItem should not be invoked.

PierBover commented 8 years ago

Ok so here it goes...

This is the JS code that loads the data:

NativeStorage.getItem("local-storage",
    storedData => {
        console.log("DATA LOADED OK");
    },
    error => {
        console.log("DATA LOADED ERROR");
        console.log('ERRORR' + error.code);
        console.log('ERRORR' + error.source);
        console.log('ERRORR' + error.exception);
    }
);

And this is what is reported by logcat with a bit of cleaning too.

08-19 14:30:30.033 12120 12120 I chromium: [INFO:CONSOLE(8673)] "LOADING OFFLINE DATA", source: file:///android_asset/www/build.js (8673)
08-19 14:30:30.037 12120 12120 I chromium: [INFO:CONSOLE(8675)] "DATA LOADED OK", source: file:///android_asset/www/build.js (8675)
08-19 14:30:30.054 12120 12120 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 12120
08-19 14:30:30.058 12120 12120 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 12120
08-19 14:30:30.073 12120 12120 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 12120
08-19 14:30:30.085 12120 12120 I chromium: [INFO:CONSOLE(8683)] "DATA LOADED ERROR", source: file:///android_asset/www/build.js (8683)
08-19 14:30:30.085 12120 12120 I chromium: [INFO:CONSOLE(8684)] "ERRORR5", source: file:///android_asset/www/build.js (8684)
08-19 14:30:30.085 12120 12120 I chromium: [INFO:CONSOLE(8685)] "ERRORRJS", source: file:///android_asset/www/build.js (8685)
08-19 14:30:30.086 12120 12120 I chromium: [INFO:CONSOLE(8686)] "ERRORRTypeError: Cannot read property 'profesor' of undefined", source: file:///android_asset/www/build.js (8686)
PierBover commented 8 years ago

I'm only storing a native JS object with strings, array, booleans, and numbers. No custom object.

PierBover commented 8 years ago

Oh, and there is no error when storing.

Here is the code that stores the data:

NativeStorage.setItem("local-storage", Config.localData, result => {
    console.log("DATA SAVED OK!");
}, error => {
    console.log("DATA SAVED ERROR!");
})

The data is saved ok:

08-19 14:42:24.596 12680 12680 I chromium: [INFO:CONSOLE(10138)] "DATA SAVED OK!", source: file:///android_asset/www/build.js (10138)
GillesC commented 8 years ago

It sounds stupid, but have you tried reinstalling the plugin. By removing it first and installing it afterwards.

And it is strange there isn't an exception in the error object. Because I explicitly put the exception in the error object.

PierBover commented 8 years ago

I forgot to paste the last line of the log... I've edited it.

It says:

08-19 14:30:30.086 12120 12120 I chromium: [INFO:CONSOLE(8686)] "ERRORRTypeError: Cannot read property 'profesor' of undefined", source: file:///android_asset/www/build.js (8686)
alokrajiv commented 8 years ago

Hi @PierBover , cud u show the part of the code where you are accessing the profesor property.

PierBover commented 8 years ago

It's super weird.

profesor is part of an object I'm importing with import MyObject from 'MyObject'.

When online I only use NativeStorage to save data and everything works fine.

When offline I read the local data from NativeStorage and then go on, but for some reason the import of MyObject isn't working. Hence the property profesor isn't available.

When offline I initialise my app on a callback on the success call of NativeStorage, which is why when there is an error the catch is being thrown on the error callback of NativeStorage.getItem().

So I guess this is something related to async hell callback, Babel, React, react-router, and whatnot... but not with NativeStorage.

Thanks for your help.

GillesC commented 8 years ago

Glad it's not a bug of nativestorage. I hope you can solve the problem.

Have a nice day.