流星客户端外部Javascript文件问题

Meteor Client Side External Javascript File issues

本文关键字:文件 问题 Javascript 外部 客户端 流星      更新时间:2023-09-26

我正在玩pubnub和Meteor,我指的是客户端外部js文件和接收错误。

我的代码如下。服务器端工作正常,但是客户端接收到错误。"未捕获的TypeError: Cannot read property '$' of undefined"

我在nodejs中工作得很好,但想让它在流星中工作。我一直在为这个烦恼,谢谢。

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
      (function(){
        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);
}

看起来您还没有初始化实例!

var pubnub = PUBNUB({
    subscribe_key: 'your-sub-key',
    publish_key: 'your-pub-key' 
});

,

pubnub.subscribe({...