Reputation: 8051
I am new to node.js and now I am facing a problem with EJS template. I noticed my for
loop is not running though the EJS template. I tried to make a very basic todo app.
Here is the hierarchy of the project I made
This is my App js module
let express = require('express');
let todoController = require('./Controller/todoController')
let app = express()
app.set('view engine', 'ejs');
todoController(app);
app.listen(3000,function(){
console.log('server started on http://localhost:3000');
})
todoController.js
let toDoList = ['Go to university','Smoking sigrate'];
module.exports= function(app){
app.get('/', function(req, res){
res.render('index.ejs', {toDoList: toDoList});
});
app.get ("*", function(req,res){
res.send("<h1>Invalid page</h1>");
})
}
index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title>Todo</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
</head>
<body>
<!-- As a heading -->
<nav class="navbar navbar-light bg-dark">
<span class="navbar-brand mb-0 h1 text-white">Todo List</span>
</nav>
<br><br>
<div class="container">
<form>
<div class="form-group text-white bg-dark">
<label for="formGroupExampleInput2">Enter to do item</label>
<input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Input a item to do list....">
<input type="submit" class="form-control bg-primary text-white h3" >
</div>
</form>
</div>
<br><br>
<div class="row">
<ul class="col-6 mx-auto" >
<% for(let i =0; toDoList.length; i++){ %>
<li> <%= toDoList[i] %> </li>
<% } %>
</ul>
</div>
</body>
</html>
Upvotes: 0
Views: 3645
Reputation: 39
you are missing the condition in looping
<% for(let i =0;i < toDoList.length; i++){ %>
<li> <%= toDoList[i] %> </li>
<% } %>
Upvotes: 2
Reputation: 119
<% for(let i =0; i<toDoList.length; i++){ %>
<li> <%= toDoList[i] %> </li>
<% } %>
use i < todoList.length. othertwise it is a infinite loop.
Upvotes: 0