skip to Main Content

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


  1. you have to import that from object like

    const { collegeModel } = require("../models/recipe.model");
    

    Or

       const clgModel = require('../modules/engg_clg');
    
       const college = clgModel.collegeModel.find({});
    
    Login or Signup to reply.
  2. You are using

    module.export = collegeModel; 
    

    in your engg.js so it is a type error just put an ‘s’ and write it:

    module.exports = collegeModel;
    
    Login or Signup to reply.
  3. Create 2 file. collegeModel.js and index.js

    1. paste this code in collegeModel.js
    var mongoose = require("mongoose")
    
    const collegeSchema = new mongoose.Schema({
      id: {
        type: Number
      },
      City: {
        type: String
      },
      Institute_ID: {
        type: String
      },
      Name: {
        type: String
      },
      Rank: {
        type: Number
      },
      Score: {
        type: Number
      },
      State: {
        type: String
      }
    })
    
    module.exports = mongoose.model("Employee", collegeSchema)
    1. Add this code in index.js
    const collegeModel = require("./collegeModel.js")
    
    router.get('/', async function(req, res, next) {
    
      const result = await collegeModel.find().lean()
    
      if (result) {
        res.render('index', {
          title: 'Engineering College Rank',
          records: data
        });
    
      } else {
        console.log("ERROR")
      }
    
    });
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search