1

I am new to pebble js app dev and want to get the json of a URL. So I came across the RSVP module however I get error for the following code:

/**
 * Welcome to Pebble.js!
 *
 * This is where you write your app.
 */

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

var Vector2 = require('vector2');
var userName = 'lamiastella';
var URL = 'http://www.last.fm/user/' + userName;
var card = new UI.Card({
  title:'last.fm stat',
  subtitle:'Fetching...'
});
card.show();


var getJSON = function(url) {
  var promise = new RSVP.Promise(function(resolve, reject){
    var client = new XMLHttpRequest();
    client.open("GET", url);
    client.onreadystatechange = handler;
    client.responseType = "json";
    client.setRequestHeader("Accept", "application/json");
    client.send();

    function handler() {
      if (this.readyState === this.DONE) {
        if (this.status === 200) { resolve(this.response); }
        else { reject(this); }
      }
    };
  });

  return promise;
};


ajax({ url: URL }, function(data){
 // var headline = data.match(/(.*?)<\/h1>/)[1];
  var title= data.match(/<h1>(.*?)<\/h1>/);
  card.subtitle(title);
  //card.subtitle(headline);
});

The error is :

[PHONE] pebble-app.js:?: JavaScript Error:
Error: Cannot find module 'rsvp'
at Object.loader.require (loader.js:34:11)
at require (loader.js:41:48)
at Object.loader (app.js:9:12)
at Object.loader.require (loader.js:44:10)
at require (loader.js:41:48)

P.S.: If using RSVP is not the way to go how can I say extract the "top artist" or similar information?

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

2 Answers2

2

I am using RSVP in my Pebble.js app. Here's how I'm requiring it:

var RSVP = require('./rsvp.js');

The only problem is that it doesn't work in the emulator, due to a missing global variable in that environment.

scribu
  • 2,958
  • 4
  • 34
  • 44
1

Unfortunately, you can't include arbitrary libraries in Pebble.js.

In your specific case RSVP is a bit of overkill, you can just nest another callback inside of the ajax request.

Kirby Kohlmorgen
  • 396
  • 1
  • 2
  • 9
  • can you please take a look at this pebble question? I even asked in Pebble IRC channel as well as PebbleDev slack but got no reply. My project is stuck testing on the pebble because I cannot find the developer options in my phone http://stackoverflow.com/questions/31418343/developers-option-in-pebble-app-settings – Mona Jalal Jul 14 '15 at 22:38
  • 1
    Hey Mona, sorry, but the tutorial that Kat posted should work. If you have more issues, then you should try emailing devsupport@getpebble.com. – Kirby Kohlmorgen Jul 16 '15 at 16:26