0

I am stuck in json data extraction in pebble.

var UI = require('ui');
var ajax = require('ajax');

var URL="http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=4a9f5581a9cdf20a699f540ac52a95c9&limit=10&format=json&callback=?";
var card = new UI.Card({
  title:'last.fm stat',
  subtitle:'Fetching...'
});
card.show();

ajax({ url: URL }, function(data){
  var topArtist=data.topartists[0].artist.name;
  card.subtitle(topArtist);
});

Here's the error I get:

[INFO] ocess_manager.c:368: Heap Usage for App <lastfm sta: Total Size <48584B> Used <6256B> Still allocated <28B>
[PHONE] pebble-app.js:?: (+) [card 1] : [card 1]

[PHONE] pebble-app.js:?: JavaScript Error:
TypeError: Cannot read property '0' of undefined
    at pebble-js-app.js:123:32
    at pebble-js-app.js:871:17
    at req.onreadystatechange (lib/ajax.js:11

4:9)

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408

1 Answers1

3

Evening Mona,

  1. Remove the question mark at the URL's end.

  2. Remove the card.show() instruction where you put it, and place it after adding a subtitle to it.

  3. Specify you're dealing with a JSON datatype.

And your final code should now look like this:

var UI = require('ui');
var ajax = require('ajax');

var URL="http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=4a9f5581a9cdf20a699f540ac52a95c9&limit=10&format=json&callback=";
var card = new UI.Card({
    title:'last.fm stat',
    subtitle:'Fetching...'
});

ajax({ url: URL, type: 'json' }, function(data) {
    var topArtist = data.topartists.artist[0].name;
    card.subtitle(topArtist);
    card.show();
});

It should now run perfectly. :)

Also, you should add a failure callback in your ajax method:

ajax({object}, success, failure)
Arnaud
  • 472
  • 4
  • 12
  • hey Arnaud, that is correct. Just a quick note, it should be var topArtist = data.topartists.artist[0].name; according to `http://www.w3resource.com/API/last.fm/tutorial.php` can you please change it in your answer because I want to accept the answer. Many thanks. P.S.: can I call getJSON in pebblejs? – Mona Jalal Jul 14 '15 at 01:13
  • 1
    No problem, it's done! I don't think you can, they designed their own methods -- as long as I can remember, jQuery isn't included. You'll need to stick to the type declaration for now. :-) – Arnaud Jul 14 '15 at 01:15
  • yeah, apparently jQuery cannot be required! I hope they add it to the new versions. – Mona Jalal Jul 14 '15 at 01:18
  • I wonder if you might know what's wrong with this? `main.on('click', 'select', function(e) { var wind = new UI.Window({ fullscreen: true, }); /* var textfield = new UI.Text({ position: new Vector2(0, 65), size: new Vector2(144, 30), font: 'gothic-24-bold', text: 'Text Anywhere!', textAlign: 'center' }); */ ajax({ url: URL, type: 'json' }, function(data) { topArtist = data.topartists.artist[1].name; }); wind.add(topArtist); wind.show(); });` – Mona Jalal Jul 14 '15 at 01:42
  • Look, you add a variable -- topArtist -- at a moment where the variable is undefined. You better add these two instructions `wind.add(topArtist); wind.show();` after assigning a value to `topArtist`. I think that's it. – Arnaud Jul 14 '15 at 02:01
  • do you know if I can run javascript in this version of pebble? can I run javascript codes on this version of Pebble? http://www.bestbuy.com/site/pebble-smartwatch-for-select-apple-and-android-devices-red/9713881.p?id=1219034549411&skuId=9713881 – Mona Jalal Jul 14 '15 at 22:45
  • also can you please take a look at this question ? I got no reply in pebble IRC channel or slack channel http://stackoverflow.com/questions/31418343/developers-option-in-pebble-app-settings Thanks – Mona Jalal Jul 14 '15 at 22:46
  • 1
    I'm no master of it Mona! :) I just had fun using PebbeJS when they released it... By the way, I believe you can run js on this version. If you're facing material issues, you should post on their forums or directly contact the Order Enquiries service. – Arnaud Jul 15 '15 at 17:05