I'm attemptng to load an external JS File


#1

My code is below.

I’m very new to Meteor so take that into account when you look at my code.

I’m attempting to load external js file and utilize it. I have an example working outside of meteor and I’m now attempting to get it to work in Meteor.

The error I’m getting is “Uncaught TypeError: Cannot read property ‘$’ of undefined”

Thanks!!!

Template.hello.rendered = function(){

  $.getScript("http://cdn.pubnub.com/pubnub.min.js", function() {
  //callbcak function
    Session.set('counter', 88);
    var output = PUBNUB.$('output');
    PUBNUB.subscribe({

      channel: 'my_channel',
      callback: function(message){
        output.innerHTML += message;
      }
    })
  }) 

};


#2

You don’t have jquery included to the project? Or your code is executing before jquery is loaded?


#3

Isn’t Jquery automagically included in Meteor projects?


#5

Hmm… I tested it myself and PUBNUB is defined. In other words is working so probably the problem is not with the code above.


#6

I’m getting the error on this line though.

var output = PUBNUB.$(‘output’);


#7

if (Meteor.isClient) {

// counter starts at 0
Session.setDefault(‘counter’, 0);

Template.hello.helpers({
counter: function () {
return Session.get(‘counter’);
}
});

Template.hello.events({
‘click button’: function () {
// increment the counter when button is clicked
Session.set(‘counter’, Session.get(‘counter’) + 1);
}
});

Template.hello.rendered = function(){

$.getScript("http://cdn.pubnub.com/pubnub.min.js", function() {
//callbcak function
  Session.set('counter', 88);
  var output = PUBNUB.$('output');
  PUBNUB.subscribe({

    channel: 'my_channel',
    callback: function(message){
      output.innerHTML += message;
    }
  })
})

};
}

if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});

var PUBNUB = Meteor.require(‘pubnub’).init({});

setInterval(function() {
PUBNUB.publish({
channel: ‘my_channel’,
message: ‘hello SMB’
});
}, 1000);
}