Skip to content

Error when persisting the value 'undefined': JSON.parse throws an exception #99

@pistillo

Description

@pistillo

When the value 'undefined' is persisted accidentally in storage, attempting to retrieve and parse it using JSON.parse throws an exception. This happens because JSON.parse('undefined') is not valid JSON, resulting in a runtime error.

storage-utils.ts

// Example retrieval code
const item = localStorage.getItem('key');
return item ? JSON.parse(item) : null; // JSON.parse(item) fails if item is 'undefined'

A potential fix could include checking explicitly for 'undefined' as a string, to avoid parsing errors:

return item && item !== 'undefined' ? JSON.parse(item) : null;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions