05-13-2018 12:28 - edited 05-13-2018 12:38
05-13-2018 12:28 - edited 05-13-2018 12:38
Hey,
sorry if I'm being stupid. I'm trying to send some data (From coinmarketcap) from my companion to my app, however when the data gets to the onmessage callback I can't seem to parse it easily.
Example: https://gist.github.com/Lawls91/a9a4af17e7484793c48ffeaf550b5044
In the code, you can see the JSON being sent, and I output it in the console log before I do. On the other side, I receive data, and when I try and output it, I get an empty console output (Not even the "App - " bit of the output displays... See consoleOutput.txt).
At first I assumed it was the formatting, however eventually I decided to delete some of the JSON and eventually it came through. So I was checking the max message length, but I am not receiving an exception as the documentation says.
Am I doing something wrong?
Thanks!
Edit: As a side note, I think some of the data (or all, cant tell) is actually there, I'm just not sure why it wont print...
Answered! Go to the Best Answer.
05-14-2018 05:35
05-14-2018 05:35
I have this function to check the properties / data of objects. Just pass it your JSON object and it will write one line to the console for each property.
So something like:
listProperties(data);
Function code:
function listProperties(object) { for(var key in object) { try{ console.log('Key: ' + key + ' | value: ' + object[key]); // recursion breaks the simulator // if ( object[key] == '[object Object]') { // listProperties(object[key], ' ' + key + '.'); // } } catch (error) { // Some values throw an error when trying to access them. console.log('Key: ' + key + ' | Error: ' + error.message); } } }
05-13-2018 18:31
05-13-2018 18:31
Here's another post that sounds similar to your problem. Maybe it will help.
https://community.fitbit.com/t5/SDK-Development/Data-error-in-messaging/td-p/2282092
05-13-2018 23:53
05-13-2018 23:53
Hey thanks for the reply. I did think this myself but I am not getting anything throw when I do:
function SendData(data) { try { console.log("Comp: " + JSON.stringify(data)); messaging.peerSocket.send(data); console.log("SENT"); } catch(err) { console.log("SEND ERROR"); } }
Can see from the console output that I get the SENT log.
Also my app is receiving the onmessage callback, and I've the fields by digging in and the data is there. It just wont print to the console? e.g. I changed the onmessage callback to:
// Listen for the onmessage event messaging.peerSocket.onmessage = function(evt) { console.log("Received message (app)!"); console.log("App: " + JSON.stringify(evt.data)); console.log("App parts1: " + evt.data.data.circulating_supply6); console.log("App parts2: " + evt.data.metadata.timestamp); }
Then my console output was:
[7:46:40 AM]Socket opened (app) [7:46:40 AM]Max message size=1027 [7:46:40 AM]Comp: {"data":{"id":1027,"name":"XYZ","symbol":"XYZ","website_slug":"XYZ","rank":2, "circulating_supply":99414514,"circulating_supply2":99414514,"circulating_supply3":99414514, "circulating_supply4":99414514,"circulating_supply5":99414514,"circulating_supply6":99414514, "last_updated":1526238557},"metadata":{"timestamp":1526238642,"error":null}} [7:46:40 AM]SENT [7:46:40 AM]Received message (app)! [7:46:40 AM] [7:46:40 AM]App parts1: 99414514 [7:46:40 AM]App parts2: 1526238642
Which looks like all the data is there? Maybe
05-14-2018 05:35
05-14-2018 05:35
I have this function to check the properties / data of objects. Just pass it your JSON object and it will write one line to the console for each property.
So something like:
listProperties(data);
Function code:
function listProperties(object) { for(var key in object) { try{ console.log('Key: ' + key + ' | value: ' + object[key]); // recursion breaks the simulator // if ( object[key] == '[object Object]') { // listProperties(object[key], ' ' + key + '.'); // } } catch (error) { // Some values throw an error when trying to access them. console.log('Key: ' + key + ' | Error: ' + error.message); } } }
05-14-2018 14:28
05-14-2018 14:28
Hey,
this did work, all my data seems to be there as I suspected. Any idea's why its not as simple to parse to a json object after its been through the peerSocket?
Thanks!
05-14-2018 15:43
05-14-2018 15:43
I don't know why it has problems.
But I do know of another gotcha; if you save aa messag to a file with fs.writefilesync(...,'json'). Then try and open as a json file with fs.readfilesync(...,'json') you'll get a string and not an object. Not sure why it has this issue; other files open correctly.