简体   繁体   中英

How can I solve this express node ejs problem?

Hello guys please I am new to node.js.I keep getting this error.Please can someone explain to me.

Error: Could not find matching close tag for "<%=".
    at /Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:727:19
    at Array.forEach (<anonymous>)
    at Template.generateSource (/Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:717:15)
    at Template.compile (/Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:571:12)
    at Object.compile (/Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:385:16)
    at handleCache (/Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:233:18)
    at tryHandleCache (/Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:272:16)
    at View.exports.renderFile [as engine] (/Users//Desktop/Web Development/getting_started_express js/node_modules/ejs/lib/ejs.js:478:10)
    at View.render (/Users//Desktop/Web Development/getting_started_express js/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users//Desktop/Web Development/getting_started_express js/node_modules/express/lib/application.js:640:10)

even after deleting ejs module and installing it again. as well as deleting my ejs file from view folder and creating a new ejs file. this time without using the "<%=" code, but yet I still get this error.

const express = require ('express');
const path = require ('path');
const ejs = require ('ejs');
const app = express();

app.use('/public',express.static(path.join(__dirname,'views')));
app.set('view engine','ejs');
app.get('/',(req,res)=>{
  res.render('index') //,{data : {userQuery: req.params.userQuery,
                              //searchResults : ['book1','book2','book3']}});
  //we can optionally pass a data in render
});

app.listen(3000);

Here is the EJS template

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>search</title>
  </head>
  <body>
    <h1>You Searched For: <%= data.userQuery %> </h1>
    <ul>
      <li>data</li>
    </ul>`

  </body>
</html>

This is the line of code giving you the error: <,-- to output on the browser page. use <%= followed by data you passed in your js. --> <,-- to output on the browser page. use <%= followed by data you passed in your js. -->

EJS doesn't care about HTML comments. EJS processes only EJS syntax, so from the EJS processor's standpoint you're missing the closing %> for the opening <%= contained in the HTML comment. HTML comments are only relevant to the browser.

You can create an EJS comment using <%# comment %> ( documentation ).

You can also have multi-line comments using <% /* */ %> ,

Original code you posted as a comment for @theParadox42 answer:

<!DOCTYPE html>
 <html lang="en" dir="ltr">
 <head>
 <meta charset="utf-8">
 <title>search</title>
 </head>
 <body>
  <h1>You Searched For: <%= data.userQuery %> </h1>
  <!-- to output on browser page, use <%= followed by data you passed in your js. 
  --> 
  <ul> 
  <li>dummy data</li> 
  <!-- <% data.searchResults.forEach(result=>{ %> <li> <%= result %> </li> <% }) %> --> 
  </ul> 
 </body> 
 </html> 

In EJS, the syntax goes as <p> X is <%= data.x %> </p> The shown error just means you have a <%= and no closing %> to match it. I would suggest searching your.ejs files for these and using search tools to track the missing one down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM