1

标题:“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)
4

1 回答 1

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});
});
于 2013-09-13T14:28:00.537 回答