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

Select function settings returns undefined or null

Hello,

I am really stuggling with how to allow someone to change a simple date format via the settings.

I copied the info verbatim a number of times from the SDK and the console logs always return undefined, null or [object OBJECT] with the exception of evt.oldValue and evt.Newvalue which log out as:

 

companion/index.js:40,3[10:28:58 a.m.]old value: {"values":[{"name":"31/12/2022"}],"selected":[1]}
companion/index.js:43,3[10:28:58 a.m.]new value: {"values":[{"name":"12/31/2022"}],"selected":[0]}
 
evt.data logs Undefined
if I log JSON.parse(evt.newValue); it logs as null. So I am trying this:
 
// Event fires when a setting is changed
settingsStorage.addEventListener("change", (evt) => {
// Which setting changed
const dateArr = JSON.parse(evt.newValue);
console.log(dateArr);
const myArr = (dateArr.values);
console.log(myArr);
console.log(myArr.name);
console.log(`val: ${evt.data}`);
 
I get somewhere but still not close enough as I still end up with null: (I tried to add labels to each log with (`xxxx ${yyyy}`); but when I do that the only thing I ever get back is [object OBJECT].
Here is what I get from above in my console.
 
companion/index.js:38,3[10:51:11 a.m.]{ values: [ { name: '31/12/2022' } ], selected: [ 1 ] } //<---dateArr
companion/index.js:37,3[10:51:11 a.m.][ { name: '31/12/2022' } ]  //<---myArr
companion/index.js:38,3[10:51:11 a.m.]null //<---myArr.name
companion/index.js:39,3[10:51:11 a.m.]val: undefined  //<---evt.data
companion/index.js:42,3[10:51:11 a.m.]old value: {"values":[{"name":"12/31/2022"}],"selected":[0]}
companion/index.js:45,3[10:51:11 a.m.]new value: {"values":[{"name":"31/12/2022"}],"selected":[1]}
 
Any advice or help would be greatly appreciated but please keep in mind I am still learning and I am not fluent in any of these languages so a what and why would be most helpful.
 
Thank you in advance.
Best Answer
0 Votes
5 REPLIES 5

You're exceptionally close!

Can I just leave a clue? values (and selected) and both arrays; that's why you see Object and undefined sometimes. Experiment with things like values[0]. You may still need to dig a bit deeper depending on what you want.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

oh I tried that but values[0] or values[1] just always return null. I went back and worked on selected to see if I would have more luck. Good news and bad news. Good news is I can get at the array position of the date format. Bad news in the messaging listener in the app index.js file always returns undefined or [object OBJECT] no matter what I do. evt.data | evt.data.value (as shown in the sdk) return and empty array of objects and evt.newValue returns undefined. Now I had that error before and when I used JSON.parse() it helped out but in this case it returns a JSON string parse error in the app index.js.

 

So I think it is still a step forward but it was into a hidden pile of quicksand

 

Best Answer
0 Votes

I suspect you're not sending a string from companion to device. Check that.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Ok...first...thank you @Gondwana for trying to help and guiding me. I sincerely appreciate it. As much I would have loved a definitive answer, it is always rewarding to find a solution rather than be given one directly.

Second. I copied directly what was out of the SDK. One would think that whomever wrote the examples in the SDK would have tested them out. One would likely be wrong more often than not I am finding.

Third: This is more stream of consciousness as a record of what I did to get it working in case someone else struggles with this. I get it may not be pretty. I get it may not be elegant but if all you need is to get from A to B a 1982 Chevette works just as well as a Ferrari.

  • I did try to send the string by commenting out the //key: key; line in the sendValue function but that got me the same old undefined errors.
  • I then rewrote all the functions to just send the evt.newValue but same result. I did verify via a console.log that I am sending just the array of objects but something was still off.
  • I then rewrote the entire sendValue function in the companion.
  • function sendValue(val) {
    if (val) {
    const dateArr = JSON.parse(val);
    const valObj = dateArr.values;
    const selectObj = dateArr.selected;
    const objDate = selectObj[0];
    sendSettingData(objDate);
    console.log(objDate);
    }
  • Then reworked my switch case
  • messaging.peerSocket.addEventListener("message", (evt) => {
    const dateFormat = evt.data;
    switch (dateFormat) {
    default: {
    lblDate.text = `${day}/${month}/${year}`;
    }
    case 0: {
    lblDate.text = `${month}/${day}/${year}`;
    break;
    }
    case 1: {
    lblDate.text = `${day}/${month}/${year}`;
    break;
    }

    }

Best Answer
0 Votes

Glad you got it going.

One common gotcha with Javascript is its loose typing. For example, it often tries to convert things to strings automatically, which is why a non-string variable may look like a string when you use console.log(). A safer way to determine a variable's type is to use typeof.

Peter McLennan
Gondwana Software
Best Answer