03-14-2018 14:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-14-2018 14:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I'm being challenged by trying to pass objects from the companion to the watch. I am continually unable to directly reference components of the object on the watch side.
Building a JSON object in the following function:
function buildGraphData(data) { // Take the data in, move a step at a time from most recent back. // look at timestamps to determine if a missed poll happened and make that graph point disappear. let obj = JSON.parse(data); let graphpointindex = 0; lastTimestamp = 0; var indexarray = []; // build the index for (var x in obj) { indexarray.push({ 'key': x, '_id': obj[x]['_id'] }); } // sort the index indexarray.sort(function (a, b) { var as = a['_id'], bs = b['_id']; return as == bs ? 0 : (as > bs ? 1 : -1); }); for (let index = 0; index < indexarray.length; index++) { if (graphpointindex <= 23) { while ((currentTimestamp - obj[indexarray[index]['key']].date) >= 300) { points[graphpointindex] = -10; currentTimestamp = currentTimestamp - 300; graphpointindex++; } points[graphpointindex] = obj[indexarray[index]['key']].sgv; graphpointindex++; if (obj[indexarray[index]['key']].date > lastTimestamp) { lastTimestamp = obj[indexarray[index]['key']].date; bgTrend = obj[indexarray[index]['key']].direction; } } } // console.log("GraphData:" + points); const messageContent = {"bgdata" : [ { "graphData": points, "lastPollTime": lastTimestamp, "currentTrend": bgTrend } ]}; console.log("CompanionData:" + messageContent); console.log("CompanionString:" + JSON.stringify(messageContent)); if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { messaging.peerSocket.send(messageContent); } }
The last two log lines before the message is sent produces the following (which at first glance looks alright to me.
- CompanionData:[object Object]
- CompanionString:{"bgdata":[{"graphData":[98,88,115,136,152,163,185,207,225,235,249,252,252,253,253,250,246,244,235,233,229,222,215,210],"lastPollTime":1521062652363,"currentTrend":"FortyFiveDown"}]
Now, on the watch I have the the receiving end of the message
messaging.peerSocket.onmessage = function(evt) { if (evt.data.hasOwnProperty("settings")) { console.log("Triggered a settings update."); updateSettings(evt.data) } else if (evt.data.hasOwnProperty("bgdata")) { console.log("Triggered a data update. " + JSON.stringify(evt.data)); const graphData = evt.data.bgdata; console.log("Is it a string?:" + graphData); console.log("Stringify:" + JSON.stringify(graphData)); updategraph(evt.data.bgdata.graphData, evt.data.bgdata.currentTrend, evt.data.bgdata.lastPollTime); } else if (evt.hasOwnProperty("theme")) { console.log("Triggered a theme update."); //This theme stuff needs a re-do, don't forget! applyTheme(evt.data.background, evt.data.foreground); let json_theme = {"backg": evt.data.background, "foreg": evt.data.foreground}; fs.writeFileSync("theme.txt", json_theme, "json"); } }
And the messages are:
- Triggered a data update. {"bgdata":[{"graphData":[98,88,115,136,152,163,185,207,225,235,249,252,252,253,253,250,246,244,235,233,229,222,215,210],"lastPollTime":1521062652363,"currentTrend":"FortyFiveDown"}]}
- Is it a string?:[object Object]
- Stringify:[{"graphData":[98,88,115,136,152,163,185,207,225,235,249,252,252,253,253,250,246,244,235,233,229,222,215,210],"lastPollTime":1521062652363,"currentTrend":"FortyFiveDown"}]
This is as far as I can get, I cannot reference evt.data.bgdata.graphData, it is always undefined. I am at the point where I think I'm missing something small/basic but I've modified how this is handled multiple times with the same outcomes. Does passing using the message API alter the data in some way I need to account for?
Answered! Go to the Best Answer.

Accepted Solutions
03-15-2018 09:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-15-2018 09:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Wasn't seeing the obvious in front of me. Namely....
const messageContent = {"bgdata" : [ { "graphData": points, "lastPollTime": lastTimestamp, "currentTrend": bgTrend } ]};
Isn't the same as...
const messageContent = {"bgdata" : { "graphData": points, "lastPollTime": lastTimestamp, "currentTrend": bgTrend } };
Making the value of bgdata an array reverted it to 0 indexed references and everything in my code fell apart.

03-15-2018 09:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-15-2018 09:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Wasn't seeing the obvious in front of me. Namely....
const messageContent = {"bgdata" : [ { "graphData": points, "lastPollTime": lastTimestamp, "currentTrend": bgTrend } ]};
Isn't the same as...
const messageContent = {"bgdata" : { "graphData": points, "lastPollTime": lastTimestamp, "currentTrend": bgTrend } };
Making the value of bgdata an array reverted it to 0 indexed references and everything in my code fell apart.

03-15-2018 09:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-15-2018 09:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
You have an array, so you need to get the first element.
const graphData = JSON.parse('{"bgdata":[{"graphData":[98,88,115,136,152,163,185,207,225,235,249,252,252,253,253,250,246,244,235,233,229,222,215,210],"lastPollTime":1521062652363,"currentTrend":"FortyFiveDown"}]}'); console.log(graphData.bgdata[0].graphData);
// [98, 88, 115, 136, 152, 163, 185, 207, 225, 235, 249, 252, 252, 253, 253, 250, 246, 244, 235, 233, 229, 222, 215, 210]

