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.
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)
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')".
@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.
For me it was a missing { on the below for loop caused the error. Look for proper closing or opening of paranthesis.
<% for(var i=0;i<users.length;i++) %>
<li> <%= users[i] %> </li>
<%}%>
</ul>
</p>
Some "hello world" examples of this error.
,
)Extra ,
between try...catch
(From the habit of "if-else" syntax).
/* Throw "message": "Uncaught SyntaxError: Missing catch or finally after try" */
try {
/* do something */
},catch (error) {
/* do something */
}
Valid code:
/* Valid code */
try {
/* do something */
}
catch (error) {
/* do something */
}
Use try without catch -or- finally.
/* Throw error: Error: Missing catch or finally after try */
try {
/* do something */
}
MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With