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

Retrieving Select Option

ANSWERED

My clock face has a Select options settings where the user picks out of a couple of options.

Currently its using this line to retrieve the selected option: settings["bottomRight"].values[0].value

where bottom right is the settingskey.

 

The problem is that whenever I sideload the clock face to my watch it doesn't recognize it and returns an error. " '0' is not defined ". But will take the command: settings["bottomRight"].

When I've changed it and ran it, it works, if i run it again it won't work and require the original command.

 

Please help.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

@mzamo wrote:

I did it, but tval = [object object] is this right? if so how do i extract the settings item i want?


Generally if you want to log an object, you need to loop through it

 

    let tVar = JSON.parse(evt.newValue)
    for (let x in tVar){
      console.log(' TYPE: ' + typeof (tVar[x]) + ' NAME: ' + x )
      if (typeof (tVar[x]) == 'object') console.log(tVar[x])
      else                              console.log('VALUE: ' + tVar[x])
    }

 

After you figure out what's in it, it's a lot easier to address the contents. In the case of selects, the selected value is:

 

console.log(tVar.values[0].value)

 

 

 

View best answer in original post

Best Answer
5 REPLIES 5

Could you be trying to access the setting before it's been set? Until a user actually interacts with a setting, it will be undefined unless you define a default for it in code.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@mzamo wrote:

My clock face has a Select options settings where the user picks out of a couple of options.

Currently its using this line to retrieve the selected option: settings["bottomRight"].values[0].value

where bottom right is the settingskey.

 

The problem is that whenever I sideload the clock face to my watch it doesn't recognize it and returns an error. " '0' is not defined ". But will take the command: settings["bottomRight"].

When I've changed it and ran it, it works, if i run it again it won't work and require the original command.

 

Please help.


I had roughly the same problem; toggles would work, colors selects would select, but selects always failed when the watchface restarted. It's hardly noticeable with the simulator, but people with actual watches expect their settings to persist.


settingsStorage works better when the values are stringified, otherwise the getItem function doesn't return the original object correctly.

 

 

settingsStorage.setItem( tKey , JSON.stringify(newValue) )

 

 

 

var tVal = JSON.parse(settingsStorage.getItem( tKey ))

 

 

Best Answer
0 Votes

I had that issue before, but it seems to have solved by using the hasOwnProperty() method

Best Answer
0 Votes

I did it, but tval = [object object] is this right? if so how do i extract the settings item i want?

Best Answer
0 Votes

@mzamo wrote:

I did it, but tval = [object object] is this right? if so how do i extract the settings item i want?


Generally if you want to log an object, you need to loop through it

 

    let tVar = JSON.parse(evt.newValue)
    for (let x in tVar){
      console.log(' TYPE: ' + typeof (tVar[x]) + ' NAME: ' + x )
      if (typeof (tVar[x]) == 'object') console.log(tVar[x])
      else                              console.log('VALUE: ' + tVar[x])
    }

 

After you figure out what's in it, it's a lot easier to address the contents. In the case of selects, the selected value is:

 

console.log(tVar.values[0].value)

 

 

 

Best Answer