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

Water Summary Data returns as "Undefined"

ANSWERED

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;
};

 

 

Best Answer
1 BEST ANSWER

Accepted Solutions

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.)

View best answer in original post

Best Answer
0 Votes
1 REPLY 1

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.)

Best Answer
0 Votes