gpt4 book ai didi

express - Jade mixin 用于 "each"/"for"循环给出 JS_Parse_Error

转载 作者:行者123 更新时间:2023-12-03 08:09:02 24 4
gpt4 key购买 nike

职称 :“each”/“for”循环中使用的 Jade mixin 给出 JS_Parse_Error

问题 : 这个代码抛出的错误的根本原因是什么?

目标 :使用 Jade 的 'mixin' 功能及其 'each' 迭代构造来使用存储在数组中的数据填充 View 。

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

代码 :

Jade View (名为“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)

最佳答案

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

您应该像这样在一行中声明它:

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});
});

关于express - Jade mixin 用于 "each"/"for"循环给出 JS_Parse_Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18707744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com