I'm attempting to make a Pebble app that reads data from a JSON URL.
However, no matter what I do I can't seem to make the data appear on the app.
When entering this code in:
console.log(data.contents.AvailableBal);
console.log(data.contents.CurrentBal);
I would expect to get a result back in the logs that would help but I only get:
[PHONE] pebble-app.js:?: None
[PHONE] pebble-app.js:?: None
And when viewing the app in the emulator, both values where the data should be, say "Undefined".
My code is as follows. Any help would be great!
var UI = require('ui');
var ajax = require('ajax');
var splashCard = new UI.Card({
title: "Please Wait",
body: "Downloading..."
});
splashCard.show();
ajax(
{
url: 'https://djkhaled.xyz/balance.json',
type: 'json'
},
function(data) {
console.log(data.contents.AvailableBal);
console.log(data.contents.CurrentBal);
var main = new UI.Card({
title: 'Balances',
body: 'Available Balance: ' + data.contents.AvailableBal +
'\nCurrent Balance: ' + data.contents.CurrentBal
});
splashCard.hide();
main.show();
}
);
This is your JSON,
You can not directly access
data.contents.AvailableBal
becauseAvailableBal
is not directly insidecontents
.contents
has an array and this array's first object (object at index 0) contains the object that hasAvailableBal
andCurrentBal
keys.I'm not a JavaScript developer but probably answer would be something like this,