05-30-2020 22:52
05-30-2020 22:52
In Storage API reference:
https://dev.fitbit.com/build/reference/companion-api/storage/#interface-storage
setItem()
setItem(key: string, value: string)
However it seems value is not restricted to string but anything would work. Especially Additive List accepts an array. I think the document here should be corrected.
Answered! Go to the Best Answer.
05-31-2020 08:32 - edited 05-31-2020 08:35
05-31-2020 08:32 - edited 05-31-2020 08:35
@BingRen wrote:In Storage API reference:
https://dev.fitbit.com/build/reference/companion-api/storage/#interface-storage
setItem()
setItem(key: string, value: string)
However it seems value is not restricted to string but anything would work. Especially Additive List accepts an array. I think the document here should be corrected.
You are correct,
However,... so is the documentation
Other types get cast to string before being stored. javaScript is also really good at casting returned numeric strings correctly for mathematical expressions. While this isn't a problem with simple types, objects (or strings containing quotes) won't 'get' correctly.
The better string conversion is to JSON.stringify on set
if (typeof value!="function")
settingsStorage.setItem( key , JSON.stringify(value) )
and JSON.parse on get
var tLen = settingsStorage.length
for ( let x=0 ; x<=tLen ; x++ ){
tKey = settingsStorage.key(x)
if ( setNames.indexOf( tKey ) > -1 ){
tSet[ tKey ] = JSON.parse(settingsStorage.getItem( tKey ))
}
}
05-31-2020 08:32 - edited 05-31-2020 08:35
05-31-2020 08:32 - edited 05-31-2020 08:35
@BingRen wrote:In Storage API reference:
https://dev.fitbit.com/build/reference/companion-api/storage/#interface-storage
setItem()
setItem(key: string, value: string)
However it seems value is not restricted to string but anything would work. Especially Additive List accepts an array. I think the document here should be corrected.
You are correct,
However,... so is the documentation
Other types get cast to string before being stored. javaScript is also really good at casting returned numeric strings correctly for mathematical expressions. While this isn't a problem with simple types, objects (or strings containing quotes) won't 'get' correctly.
The better string conversion is to JSON.stringify on set
if (typeof value!="function")
settingsStorage.setItem( key , JSON.stringify(value) )
and JSON.parse on get
var tLen = settingsStorage.length
for ( let x=0 ; x<=tLen ; x++ ){
tKey = settingsStorage.key(x)
if ( setNames.indexOf( tKey ) > -1 ){
tSet[ tKey ] = JSON.parse(settingsStorage.getItem( tKey ))
}
}
05-31-2020 17:55
05-31-2020 17:55
Now I see the point. Thank you.
Also I tried in a test app and if I pass in an array, there's an assertion error. I need to use JSON.stringify to make it work properly. So the documentation is correct.
Thanks again!