简体   繁体   中英

Coffeescript variable concatenation causes "Uncaught TypeError value is not a function"

I'm creating an app in Rails that uses ActionCable/Websockets and I'm using coffeescript to update a users browser with data received from the current channel in ActionCable. When I run this code I get the error "Uncaught TypeError: data.user.name is not a function", however when I swap data.user.id and data.user.name the error is then for data.user.id. I think my syntax is wrong but I can't find any answers. Any help would be appreciated. Thanks

received: (data) ->
  $('#players-table').show()
  $('#players-table-body').append '<tr>' + 
    '<td>' + data.user.id + '</td>' + 
    '<td>' + data.user.name +'</td>'

It'll sound strange, but you need an space after the last + :

received: (data) ->
  $('#players-table').show()
  $('#players-table-body').append '<tr>' + 
    '<td>' + data.user.id + '</td>' +
    '<td>' + data.user.name + '</td>'

Without it, it'd make the data.user.name to open parenthesis and to put inside the +'</td>' , like:

return $('#players-table-body').append('<tr>' + '<td>' + data.user.id + '</td>' + '<td>' + data.user.name(+'</td>'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM