Jade代码中的意外标记

时间:2016-03-08 11:25:06

标签: node.js pug

我有以下Jade文件,我发现here

extends layout

block scripts
  script(type='text/javascript', src='/socket.io/socket.io.js')
  script(type='text/javascript')
  var socket = io.connect('http://localhost:8080');
  socket.on('chat', function(data) {
    document.getElementById('chat').innerHTML =
      '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
  });
  var submitChat = function(form) {
    socket.emit('chat', {text: form.chat.value});
    return false;
  };

block content
  div#chat

  form(onsubmit='return submitChat(this);')
    input#chat(name='chat', type='text')
    input(type='submit', value='Send Chat')

不幸的是,我得到了以下错误按摩:

SyntaxError: /home/lorencm/Downloads/book-node-mongodb-backbone/ch02/views/chat.jade:10
  8|       document.getElementById('chat').innerHTML =
  9|         '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
> 10|     });
  11|     var submitChat = function(form) {
  12|       socket.emit('chat', {text: form.chat.value});
  13|       return false;

Unexpected token ;
  at Function (native)
  at assertExpression (/home/lorencm/Downloads/node_modules/jade/lib/lexer.js:30:3)
  at Object.Lexer.attrs (/home/lorencm/Downloads/node_modules/jade/lib/lexer.js:676:20)
  at Object.Lexer.next (/home/lorencm/Downloads/node_modules/jade/lib/lexer.js:939:15)
  at Object.Lexer.lookahead (/home/lorencm/Downloads/node_modules/jade/lib/lexer.js:113:46)
  at Parser.lookahead (/home/lorencm/Downloads/node_modules/jade/lib/parser.js:102:23)
  at Parser.peek (/home/lorencm/Downloads/node_modules/jade/lib/parser.js:79:17)
  at Parser.tag (/home/lorencm/Downloads/node_modules/jade/lib/parser.js:773:22)
  at Parser.parseTag (/home/lorencm/Downloads/node_modules/jade/lib/parser.js:759:17)
  at Parser.parseExpr (/home/lorencm/Downloads/node_modules/jade/lib/parser.js:211:21) 

我做错了什么?附:我使用的是最新的Express,Jade和Socket.io版本?

1 个答案:

答案 0 :(得分:1)

你应该缩进你的脚本。

&#13;
&#13;
block scripts
  script(src='/socket.io/socket.io.js')
  script.
    var socket = io.connect('http://localhost:8080');
    socket.on('chat', function(data) {
      document.getElementById('chat').innerHTML =
        '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
    });
    var submitChat = function(form) {
      socket.emit('chat', {text: form.chat.value});
      return false;
    };
&#13;
&#13;
&#13;