mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.71k stars 846 forks source link

SyntaxError: Unexpected token ')' in C:.../.../ while compiling ejs #631

Closed Dialtor closed 2 years ago

Dialtor commented 2 years ago

ROUTES

const router = require('express').Router(); const express = require('express'); const useExpress = express(); const bodyParser = require('body-parser');

useExpress.use(bodyParser.json()); useExpress.use(bodyParser.urlencoded({extended:true}));

const Cliente = require('../models/Clientes'); const Registro = require('../models/Registros');

router.get('/',(req, res) => {
    res.render('login');
});

router.get('/login', (req,res) =>{
    res.render('login');
})

router.get('/index', (req, res) =>{
    res.render('index');
})

router.get('/Clientes', (req, res) =>{
    res.render('Clientes')
})

router.get('/ClientesRegistro',  async (req, res) =>{
    const clientes = await Cliente.find();
    console.log(clientes);
    res.render('ClientesRegistro', {clientes: clientes})
})

router.post('/ClientesRegistro', async(req,res) =>{ const newCliente = new Cliente(req.body); await newCliente.save(); res.redirect('/Clientes'); });

module.exports = router;

HTML

<% clientes.forEach((function (cliente)) { %>

              <tr>
                <th scope="row"><%= cliente._id %></th>
                <td><%= cliente.nombreCliente %></td>
                <td><%= cliente.cedulaCliente %></td>
                <td><%= cliente.direccionCliente %></td>
                <td><%= cliente.celularCliente %></td>
                <td><%= cliente.correoCliente %></td>
                <td>
                  <button type="button" class="btn btn-primary"><i class="far fa-eye"></i></button>
                  <button type="button" class="btn btn-success"><i class="fas fa-edit"></i></button>
                <button type="button" class="btn btn-danger"><i class="far fa-trash-alt"></i></button>
                </td>
              </tr>

               <% }) %>`

I trying use ejs but I can't finish for this :(

mde commented 2 years ago

I would start debugging with a very simple forEach loop, and then slowly add the rest until you figure out what is breaking.

Starting with a very minimal test ensure that EJS itself is working:

(File test.ejs)

<% ['a', 'b', 'c'].forEach((item) => { %>
  <p><%= item; %></p>
<% }); %>

In Node repl:

> let ejs = require('./ejs');
undefined
> let tmpl = require('fs').readFileSync('./test.ejs').toString();
undefined
> ejs.render(tmpl);
'\n  <p>a</p>\n\n  <p>b</p>\n\n  <p>c</p>\n\n\n\n'

This should give you enough info to start debugging your app code. This is not a problem with EJS. I am closing this issue. Please feel free to open a new issue if there is a problem with EJS itself.