Am I "getting" the data correctly? I just want to display the total water I've logged so far that day. I print the results to the console and I get "{ summary: {} }" in companion and "undefined" in app.
companion/index.js
fetch(`https://api.fitbit.com/1/user/-/foods/log/water/date/${todayDate}.json`, {
method: "GET",
headers: {"Authorization": `Bearer ${accessToken}`}
})
.then(function(res) {return res.json();})
.then(function(data) {
let myData = {summary: {water:data.summary}};
console.log(myData);
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
messaging.peerSocket.send(myData);
}
})
app/index.js
// Message is received from companion
messaging.peerSocket.onmessage = evt => {
console.log(evt.data.water);
myWater.text = evt.data.water;
};
Answered! Go to the Best Answer.
Best AnswerAfter trying different combinations of words, I accidentally stumbled upon the right format. The Web API formatting was no help explaining the reason behind the syntax.
The proper way to call the data would be:
let myData = {"water":data.summary.water};
Then to display that data:
myWater.text = evt.data.water; //the last word is what you wrote in the quotation marks in Companion.
(I wrote "blah" in the quotation marks and called "evt.data.blah" and it worked just fine.)
Best AnswerAfter trying different combinations of words, I accidentally stumbled upon the right format. The Web API formatting was no help explaining the reason behind the syntax.
The proper way to call the data would be:
let myData = {"water":data.summary.water};
Then to display that data:
myWater.text = evt.data.water; //the last word is what you wrote in the quotation marks in Companion.
(I wrote "blah" in the quotation marks and called "evt.data.blah" and it worked just fine.)
Best Answer