ReferenceError: students is not defined
hello m new to nodejs and trying to fetch students data at index of attendance but it says: students is not defined this is folder structure https://ibb.co/d5Q0GwR
error:
ReferenceError: D:\nodejs\node_project\views\attendance\index.ejs:17
15| </thead>
16| <tbody>
>> 17| <% students.forEach(function (student) { %>
18| <tr>
19| <td>Tiger Nixon</td>
20| <td><%= student.student_name %></td>
students is not defined
controller:
const express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const Attendance = mongoose.model('Attendance');
router.get('/', (req, res) => res.render('attendance/index'));
router.get('/student', (req, res) => {
Student.find((err, docs) => {
if (!err) { res.render("student", {
students: docs
});
} else {
console.log('Error in retrieving students record :' + err);
}
});
});
module.exports = router;
student controller:
paste.ofcode.org/HkXdi3vxZaYPGd4Rx9mLKg
Solution 1:
When you access router.get('/student'
you pass students
into the EJS with res.render("student", { students: docs })
.
When you access router.get('/'
you just say res.render('attendance/index'))
and don't pass anything into it, let alone an object that defines students.
You need to do there what you do in /student
.