Not able to to figure out why its "find is not a function" I am new
at Nodejs and Mongodb. want to show database document using ejs Please help me out. I checked connection to DB, it’s ok. I also read some other questions but any suitable answer for my problem. It showing
TypeError: clgModel.find is not a function
index.js
var express = require('express');
var clgModel = require('../modules/engg_clg');
var router = express.Router();
var college = clgModel.find({});
/* GET home page. */
router.get('/', function(req, res, next) {
college.exec(function(err, data) {
if (err) throw err;
res.render('index', {
title: 'Engineering College Rank',
records: data
});
});
});
module.exports = router;
**
engg.js
**
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/Engg_clg_rnk', {
useNewUrlParser: true
});
var conn = mongoose.connection;
var collegeSchema = new mongoose.Schema({
id: Number,
City: String,
Institute_ID: String,
Name: String,
Rank: Number,
Score: Number,
State: String`enter code here`
});
var collegeModel = mongoose.model('Employee', collegeSchema);
module.export = collegeModel;
EJS
showing the database document in tabular form
<thead>
<tr>
<th scope="col">City</th>
<th scope="col">Institute_ID</th>
<th scope="col">Name</th>
<th scope="col">Rank</th>
<th scope="col">Score</th>
<th scope="col">State</th>
</tr>
</thead>
<% records.forEach(function(row){%>
<tr>
<td><%= row.City %></td>
<td><%= row.Institute_ID %></td>
<td><%= row.Name %></td>
<td><%= row.Rank %></td>
<td><%= row.Score %></td>
<td><%= row.State %></td>
</tr>
<% }) %>
</table>
3
Answers
you have to import that from object like
Or
You are using
in your
engg.js
so it is a type error just put an ‘s’ and write it:Create 2 file.
collegeModel.js
andindex.js
collegeModel.js
index.js