ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Storage getJson() throws exception

Open xelamak opened this issue 10 years ago • 2 comments

Short description of the problem:

This error occurs after I have updated Angular2 to 2.0.0-beta.14 and ionic-angular to 2.0.0-beta.4. Whenever using SQLStorage's getJson to get a non-existent value, Angular2 caught the error it throws (EXCEPTION: Error: Uncaught (in promise): SyntaxError: Unexpected token u) and blocked the app.

What behavior are you expecting?

The getJson return undefined instead of throwing exception.

Steps to reproduce:

  1. Use getJson to get a non-existent value.
Typescript:
public static demo(): Promise<any> {
    var storage = new Storage(SqlStorage);
    return storage.getJson('any key of non-existent value');
}

Other information: Suggestion how to fix: In ionic-angular/platform/storage/storage.js:

    Storage.prototype.getJson = function (key) {
        return this.get(key).then(function (value) {
            try {
                return (value) ? JSON.parse(value) : value; // <-- Simply check if the value is undefined before parsing.
            }
            catch (e) {
                void 0;
                throw e; // rethrowing exception so it can be handled with .catch()
            }
        });
    };

p.s. If it is intended to throw an exception in this case, then may be add a function .has(key) to allow the app check the key before running any get functions?

Which Ionic Version? 2.0.0-beta.4

Plunker that shows an example of your issue

http://plnkr.co/edit/zpX9kg8R8Pr0mrvPi31n?p=info

Run ionic info from terminal/cmd prompt: (paste output below) Cordova CLI: 6.1.0 (cordova-lib@undefined) Gulp version: CLI version 3.9.1 Gulp local: Local version 3.9.1 Ionic Version: 2.0.0-beta.4 Ionic CLI Version: 2.0.0-beta.23 Ionic App Lib Version: 2.0.0-beta.13 OS: Node Version: v5.7.1

xelamak avatar Apr 09 '16 05:04 xelamak

+1

OscarGalindo avatar Apr 09 '16 14:04 OscarGalindo

Hello! As this issue is a duplicate of #6079 i will be closing this issue. Feel free to chime in over on that issue! Thanks!

jgw96 avatar Apr 11 '16 14:04 jgw96