Local Storage Date is saved as String
-
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?
-
@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.
-
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.
-
@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 notits 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