在“each”/“for”循环中使用的jade mixin给出了JS_Parse_Error

时间:2013-09-09 22:02:00

标签: express syntax-error pug mixins uglifyjs

标题:“each”/“for”循环中使用的jade mixin给出了JS_Parse_Error

问题:此代码引发错误的原因是什么?并修复了什么?

目标:使用Jade的'mixin'功能及其'each'迭代结构,使用存储在数组中的数据填充视图。

背景:假设问题是迭代语法,我尝试了很多排列('each'|'for';逗号|没有逗号;(占位符|表达式)插值,转义|未转义;改变空白)......甚至祈祷。我完全没有想法。

代码

Jade视图(名为“mixin_with_args.jade”的文件放在$ APP_ROOT / views /中):

heroes = [
  {name: 'Fooman', role: 'captain'},
  {name: 'Barman', role: 'entertainer'},
  {name: 'Napman', role: 'hacker'},
  {name: 'Zipman', role: 'collector'}
]

mixin heroes_list(hero)
  if hero.role == 'captain'
    li Captain #{hero.name}
  else
    li #{hero.name}

ul
  each hero in heroes
    +heroes_list(hero)

express.js服务器“app.js”(放在$ APP_ROOT中):

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger({ format: ':remote-addr :method :url' }));
app.use(express.bodyParser());
app.use(express.static('./public'));
app.use(app.router);

app.locals.pretty = true;
app.use(express.errorHandler({ dumpExceptions: true , showStack: true }));

app.get('/', function(req, res) {
  res.render('mixin_with_args', {
  });
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

错误消息和堆栈跟踪是:

500 Unexpected token: punc ()) (line: 4, col: 15, pos: 185) 
Error at new JS_Parse_Error (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:196:18) 
at js_error (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:204:11) 
at croak (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:636:9) 
at token_error (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:644:9) 
at unexpected (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:650:9) 
at expr_atom (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1119:13) 
at maybe_unary (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1287:19) 
at expr_ops (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1322:24) 
at maybe_conditional (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1327:20) 
at maybe_assign (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1351:20)
at new JS_Parse_Error (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:196:18)
at js_error (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:204:11)
at croak (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:636:9)
at token_error (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:644:9)
at unexpected (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:650:9)
at expr_atom (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1119:13)
at maybe_unary (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1287:19)
at expr_ops (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1322:24)
at maybe_conditional (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1327:20)
at maybe_assign (/Users/lucky/myapp/node_modules/jade/node_modules/with/node_modules/uglify-js/lib/parse.js:1351:20)

1 个答案:

答案 0 :(得分:1)

希望这会对你有所帮助。你的问题不是你如何迭代循环。这很好用。您正在处理的问题是如何声明变量数组。

你应该在一行中声明它:

heroes = [{name: 'Fooman', role: 'captain'}, {name: 'Barman', role: 'entertainer'},{name: 'Napman', role: 'hacker'},{name: 'Zipman', role: 'collector'}]

mixin heroes_list(hero)
  if hero.role == 'captain'
    li Captain #{hero.name}
  else
    li #{hero.name}

ul
  each hero in heroes
    +heroes_list(hero)

请参阅:https://github.com/visionmedia/jade/issues/502

如果这对您来说不是一个有吸引力的解决方案,那么您可以在app.js中声明您的数组并将其传递给渲染。

app.get('/', function(req, res) {
  var heroes = [
    {name:"Fooman", role: "captain"},
    {name:"Barman", role: "entertainer"}
  ];
  res.render('mixin_with_args', {heroes: heroes});
});