javascript 未捕获或最终尝试后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13785740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing Catch or Finally After Try
提问by netpoetica
I'm using Node, Express with EJS view engine, nano (for couchdb), and I'm running into this pretty puzzling error which I cold not find any Node/JS specific info about via SO or Goog. The area of my code that seems to be triggering this involves nested async callbacks which grab a document from CouchDB, look through it for some matches, and push an object to an array in my private scope.
我正在使用 Node、Express 和 EJS 视图引擎 nano(用于 couchdb),我遇到了这个非常令人费解的错误,我没有通过 SO 或 Goog 找到任何关于 Node/JS 的特定信息。我的代码中似乎触发了这个的区域涉及嵌套的异步回调,它从 CouchDB 中抓取一个文档,查看它是否有一些匹配项,然后将一个对象推送到我的私有范围内的一个数组。
And the error:
和错误:
Express
500 SyntaxError: Missing catch or finally after try
at Object.Function (unknown source)
at exports.compile (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/ejs/lib/ejs.js:234:12)
at Object.exports.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/ejs/lib/ejs.js:273:10)
at View.exports.renderFile [as engine] (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/ejs/lib/ejs.js:303:22)
at View.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/express/lib/view.js:75:8)
at Function.app.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/express/lib/application.js:501:10)
at ServerResponse.res.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/express/lib/response.js:719:7)
at exports.dashboard.res.render.msg (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/routes/index.js:19:29)
at module.exports.read (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/api/Character.js:56:25)
at Request._callback (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/nano/nano.js:296:11)
回答by netpoetica
The error was a missing bracket on an if statement in my EJS template. As that trace shows, it was in the exports.compile function (which is called by res.render()) that this error occurs) - the lib author uses a string to create a new function, which encloses my EJS file functionality in a try block, which becomes a dangling try because my missing opening bracket in my if block caused a syntax error in the new anonymous function created as a string and built with "new Function('str')".
错误是我的 EJS 模板中的 if 语句缺少括号。正如该跟踪所示,正是在exports.compile 函数(由res.render() 调用)中发生了此错误)——lib 作者使用一个字符串来创建一个新函数,该函数将我的 EJS 文件功能包含在一个try 块,这变成了一个悬空尝试,因为我的 if 块中缺少左括号导致新匿名函数出现语法错误,该函数创建为字符串并使用“new Function('str')”构建。
@T.J.Crowder pointed out, that stack trace is perfectly clear, and did ultimately lead to this solution. I removed some of my example code because it definitely wasn't linked to the ultimate problem.
@TJCrowder 指出,堆栈跟踪非常清楚,并最终导致了这个解决方案。我删除了一些示例代码,因为它绝对与最终问题无关。
回答by Jinesh Mathew
For me it was a missing { on the below for loop caused the error. Look for proper closing or opening of paranthesis.
对我来说,下面的 for 循环中缺少 { 导致了错误。寻找适当的关闭或打开括号。
<% for(var i=0;i<users.length;i++) %>
<li> <%= users[i] %> </li>
<%}%>
</ul>
</p>