javascript - 'jsonp' 未定义(带有 PubNub 的 AngluarJS)

标签 javascript angularjs scope pubnub

我正在学习 Angular 并使用 PubNub 库创建聊天应用程序,然后当我导航到可以创建房间或加入聊天的页面时出现此错误。

    TypeError: Cannot read property 'jsonp' of undefined
        at window.PUBNUB.CREATE_PUBNUB (https://cdn.pubnub.com/pubnub-3.7.20.js:2719:14)
        at new SELF (https://cdn.pubnub.com/pubnub-3.7.20.js:2739:16)
        at a.exports.e.value (http://localhost:9000/bower_components/pubnub-angular/dist/pubnub-angular-3.1.0.min.js:16:64)
        at Object.b.init (http://localhost:9000/bower_components/pubnub-angular/dist/pubnub-angular-3.1.0.min.js:4:87)
        at new <anonymous> (http://localhost:9000/scripts/controllers/main.js:13:15)
        at invoke (http://localhost:9000/bower_components/angular/angular.js:4535:17)
        at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:4543:27)
        at http://localhost:9000/bower_components/angular/angular.js:9395:28
        at link (http://localhost:9000/bower_components/angular-route/angular-route.js:977:26)
        at invokeLinkFn (http://localhost:9000/bower_components/angular/angular.js:9039:9) <div ng-view="" class="ng-scope" data-ng-animate="1">(anonymous function) @ angular.js:12722(anonymous function) @ angular.js:9490invokeLinkFn @ angular.js:9041nodeLinkFn @ angular.js:8533compositeLinkFn @ angular.js:7929publicLinkFn @ angular.js:7809boundTranscludeFn @ angular.js:7947controllersBoundTransclude @ angular.js:8560update @ angular-route.js:935Scope.$broadcast @ angular.js:16573(anonymous function) @ angular-route.js:619processQueue @ angular.js:14991(anonymous function) @ angular.js:15007Scope.$eval @ angular.js:16251Scope.$digest @ angular.js:16069Scope.$apply @ angular.js:16359done @ angular.js:10791completeRequest @ angular.js:10989requestLoaded @ angular.js:10930

这不是什么意思,需要帮助吗?

我的代码在这里

angular.module('pnChatApp')
 .controller('MainCtrl', ['$scope', '$rootScope', '$location', 'Pubnub', function ($scope, $rootScope, $location, Pubnub) {
    var _ref;
    if (!Pubnub.init()) {
        $location.path('/join');
    }

    $scope.controlChannel = '__controlChannel';

    $scope.channels = [];

    //Publish Chat
    $scope.publish = function() {
        if (!$scope.selectedChannel) {
            return;
        }
        Pubnub.ngPublish({
            channel: $scope.selectedChannel,
            message: {
                text: $scope.newMessage,
                user: $scope.data.username
            }
        });
        return $scope.newMessage = '';
    };

  //Create Channel
  $scope.createChannel = function() {
    var channel;
    console.log('Creating Channel...');
    channel = $scope.newChannel;
    $scope.newChannel = '';
    Pubnub.ngGrant( {
        channel: channel,
        read: true,
        write: true,
        callback: function(){
            return console.log(channel + 'All Set', arguments);
        }
    });
    Pubnub.ngGrant( {
        channel: channel + '-pnpres',
        read: true,
        write: false,
        callback: function(){
            return console.log(channel + 'Presence All Set', arguments);
        }
    });
    Pubnub.ngPublish({
        channel: $scope.controlChannel,
        message: channel
    });
    return setTimeout(function() {
        $scope.subscribe(channel);
        return $scope.showCreate = false;
    }, 100);
  };




  $scope.subscribe = function(channel) {
    var _ref;
    console.log('Subscribing...');
    if (channel === $scope.selectedChannel) {
      return;
    }
    if ($scope.selectedChannel) {
      Pubnub.ngUnsubscribe({
        channel: $scope.selectedChannel
      });
    }
    $scope.selectedChannel = channel;
    $scope.messages = ['Welcome to ' + channel];
    Pubnub.ngSubscribe({
      channel: $scope.selectedChannel,
      state: {
        "city": ((_ref = $rootScope.data) !== null ? _ref.city : void 0) || 'unknown'
      },
      error: function() {
        return console.log(arguments);
      }
    });

    $rootScope.$on(Pubnub.ngPrsEv($scope.selectedChannel), function(ngEvent, payload) {
      return $scope.$apply(function() {
        var newData, userData;
        userData = Pubnub.ngPresenceData($scope.selectedChannel);
        newData = {};
        $scope.users = Pubnub.map(Pubnub.ngListPresence($scope.selectedChannel), function(x) {
          var newX;
          newX = x;
          if (x.replace) {
            newX = x.replace(/\w+__/, "");
          }
          if (x.uuid) {
            newX = x.uuid.replace(/\w+__/, "");
          }
          newData[newX] = userData[x] || {};
          return newX;
        });
        return $scope.userData = newData;
      });
    });

    Pubnub.ngHereNow({
      channel: $scope.selectedChannel
    });

    $rootScope.$on(Pubnub.ngMsgEv($scope.selectedChannel), function(ngEvent, payload) {
     var msg;
     msg = payload.message.user ? "[" + payload.message.user + "]" + payload.message.text : payload.message.text;
     return $scope.$apply(function() {
      return $scope.messages.unshift(msg);
    });
   });

    return Pubnub.ngHistory({
     channel: $scope.selectedChannel,
     auth_key: $scope.authKey,
     count: 500
   });
  };




  Pubnub.ngSubscribe({
    channel: $scope.controlChannel
  });
  $rootScope.$on(Pubnub.ngMsgEv($scope.controlChannel), function(ngEvent, payload) {
    return $scope.$apply(function() {
     if ($scope.channels.indexOf(payload.message) < 0) {
      return $scope.channels.push(payload.message);
    }
  });
  });
  return Pubnub.ngHistory({
    channel: $scope.controlChannel,
    count: 500
  });
  $scope.channels = 'TheWaitingRoom';
  return $scope.createChannel();

}]);

最佳答案

jsonp 错误的原因是 Pubnub.init 有必需的选项参数

Pubnub.init({ publish_key: 'your pub key', subscribe_key: 'your sub key' });

https://github.com/pubnub/pubnub-angular

关于javascript - 'jsonp' 未定义(带有 PubNub 的 AngluarJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986481/

相关文章:

javascript - moment-timezone 将 utc 更改为默认时区

javascript - 如何将变量作为字符串注入(inject)?

javascript - Chrome jQuery 表单插件跨域安全漏洞?

javascript - while 元音计数器循环

angularjs - 如何验证 Angular HTML 模板

angularjs - AngularJS Controller 是否使用过不止一种操作/设置方法?

python - Python 类中的作用域

javascript - 从javascript中的匿名函数访问类成员

javascript - 使用 Angular ng-repeat 访问第三层 JSON

matlab - 我们可以配置 MATLAB 让变量具有最小局部作用域吗?