Navigation

    Quasar Framework

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Local Storage Date is saved as String

    Framework
    3
    4
    20
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      Tom De Smet last edited by

      Hi, according to the docs, types are kept when saving/retrieving from Local Storage with Quasar.
      I’m saving an object that has other types inside. It has another Object a Date and some Strings in it.
      The Object inside the Object is returned as such, but the Date is returned as a String.

      Is this a bug or a consequence of it being inside an Object? And if so, why are Objects inside Objects not returned as Strings?

      beets 1 Reply Last reply Reply Quote 0
      • beets
        beets @Tom De Smet last edited by beets

        @Tom-De-Smet Confirmed it encodes a nested date as a string based on this simple code:

              this.$q.localStorage.set('date', new Date())
        
              this.$q.localStorage.set('test', {
                nested: { foo: 'bar' },
                date: new Date(),
                string: 'hello world'
              })
              
              const date = this.$q.localStorage.getItem('date') // Correctly returns date
              const test = this.$q.localStorage.getItem('test') // Returns date as string
              console.log(date)
              console.log(test)
        

        Relevant code is here: https://github.com/quasarframework/quasar/blob/dev/ui/src/utils/web-storage.js#L3

        Quasar only checks the root element to see what type it is, and doesn’t recursively check. Maybe it should, but I can also see why the authors wanted to keep things slightly simpler. You may want to open a github issue to see if they think its something Quasar should support.

        This is also something you could always implement yourself, using something like: https://www.npmjs.com/package/json-stringify-date

        Basically, just stringify the object yourself and store it as a string, then use the same library to parse it.

        1 Reply Last reply Reply Quote 0
        • dobbel
          dobbel last edited by

          This is not mentioned in the docs, in the contrary:

          Quasar retrieves data with its original data type

          https://quasar.dev/quasar-plugins/web-storage#LocalStorage-API

          I am surprised nobody has every mentioned it before on the forum.

          beets 1 Reply Last reply Reply Quote 0
          • beets
            beets @dobbel last edited by

            @dobbel If my response wasn’t clear, I’d agree this is definitely a bug in the documentation at least. Although I guess it does say its original data type and not its original data types ^^

            I think recursively serializing an object would be nice, and actually looks like it’s being worked on: https://github.com/pdanpdan/quasar/commit/5b25d0f9a58503553d9f0d1a659cba3116172125

            1 Reply Last reply Reply Quote 0
            • First post
              Last post