mongodb TypeError:find不是函数

iezvtpos  于 2023-01-12  发布在  Go
关注(0)|答案(3)|浏览(173)

无法弄清楚为什么它的"查找不是一个函数"我是Nodejs和Mongodb的新手。想使用ejs显示数据库文档请帮助我。我检查了到DB的连接,它是确定的。我还阅读了一些其他问题,但任何适合我的问题的答案。它显示类型错误:clgModel.find不是函数

    • 索引. 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;

**
    • 工程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;
    • 一月一日**

以表格形式

<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>

显示数据库文档

rqmkfv5c

rqmkfv5c1#

你必须从这样的对象导入

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

或者

const clgModel = require('../modules/engg_clg');

   const college = clgModel.collegeModel.find({});
mlnl4t2r

mlnl4t2r2#

创建2个文件. collegeModel.jsindex.js
1.将此代码粘贴到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.将此代码添加到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")
  }

});
fslejnso

fslejnso3#

您正在使用

module.export = collegeModel;

在您engg.js中,因此这是一个类型错误,只需输入“s”并将其写入:

module.exports = collegeModel;

相关问题