Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

setItem(key: string, value: string) in Storage API - is the value really restricted to string?

ANSWERED

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.

Best Answer
1 BEST ANSWER

Accepted Solutions

@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 ))
  }
}

 

 

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

@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 ))
  }
}

 

 

Best Answer
0 Votes

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!

Best Answer
0 Votes