javascript - TypeError : App. 电缆未定义

标签 javascript ruby-on-rails coffeescript actioncable

我在计算机上运行 ActionCable 时遇到问题。

在 asset/javascripts/channels 中我有两个文件:

索引.咖啡

#= require action_cable
#= require_self
#= require_tree .

@App = {}
App.cable = ActionCable.createConsumer()

和question.coffee

App.question = App.cable.subscriptions.create "QuestionChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    # Called when there's incoming data on the websocket for this channel

  follow: ->
    @perform 'follow'

  unfollow: ->
    @perform 'unfollow'

我的 application.js 文件如下所示:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require icheck
//= require homer/scripts
//= require Chart
//= require excanvas
//= require channels
//= require_tree .

当我启动 Cable 服务器和 Rails 服务器并在 Firefox 控制台中访问 localhost:3000 时,我看到两个错误:

SyntaxError: An invalid or illegal string was specified


this.webSocket = new WebSocket(this.consumer.url);

TypeError: App.cable is undefined


App.question = App.cable.subscriptions.create("QuestionChannel", {

我使用的是 Rails 5 beta 3。我该如何解决这个问题?

最佳答案

ActionCable.createConsumer 需要一个参数 - Action Cable 服务器的地址。

@App = {}
App.cable = ActionCable.createConsumer("ws://cable.example.com")

如果您错过此参数,则 this.customer.url 未定义。

关于javascript - TypeError : App. 电缆未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772546/

相关文章:

javascript - 为什么我会收到此 Cloud Firestore 功能错误?

ruby-on-rails - 在Rails的文本框中格式化DateTime

javascript - 使用 Eclipse 突出显示 Coffeescript 文本

node.js - 当node-amqp时也不异常(exception)

node.js - PM2 - 使用 NodeJS 参数运行 Coffeescript 文件

javascript - 使用正则表达式改进递归搜索

javascript - 使用 lodash 检查空对象返回 false

javascript - 我在 Jquery 中的变量没有按我想要的方式工作

ruby-on-rails - FactoryGirl 在数据库中找不到实例

ruby-on-rails - 如何在 update_attributes 设置为 false 的情况下 stub `current_user`?