01-03-2022 15:26 - edited 01-03-2022 15:45
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-03-2022 15:26 - edited 01-03-2022 15:45
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.

- Labels:
-
JavaScript
-
Web
Accepted Solutions
01-04-2022 01:47 - edited 01-04-2022 01:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-04-2022 01:47 - edited 01-04-2022 01:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
After 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.)

01-04-2022 01:47 - edited 01-04-2022 01:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-04-2022 01:47 - edited 01-04-2022 01:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
After 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.)

