Home > Software design >  How Referencing two schema in Mongoose
How Referencing two schema in Mongoose

Time:11-06

if I have two schemas like: faculty % department how should I connect those two,

I'm getting this error

throw new TypeError('Router.use() requires a middleware function but got a '   gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a Object

My files

faculty.js

const mongoose = require("mongoose");

const facultySchema = new mongoose.Schema({
    facultyCode: {
        type: String,
        required: true,
        unique: true
      },
      facultyName: {
        type: String,
        required: true,
        unique: true
      }
});

module.exports = mongoose.model("Faculty", facultySchema);

departmentSchema.js

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const departmentSchema = new mongoose.Schema({
    departmentName: {
        type: String,
        required: true,
        unique: true
      },
      departmentCode: {
        type: String,
        required: true,
        unique: true
      },
      facultyId: {
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'facultySchema'
      }
});

module.exports = mongoose.model("Department", departmentSchema);

error code form vsCode

C:\Users\User\Documents\00-System\Backend\node_modules\express\lib\router\index.js:458
      throw new TypeError('Router.use() requires a middleware function but got a '   gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a Object
    at Function.use (C:\Users\User\Documents\00-System\Backend\node_modules\express\lib\router\index.js:458:13)
    at Function.<anonymous> (C:\Users\User\Documents\00-System\Backend\node_modules\express\lib\application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (C:\Users\User\Documents\00-System\Backend\node_modules\express\lib\application.js:217:7)
C:\Users\User\Documents\00-System\Backend\nod

CodePudding user response:


// routes/department.js missing route export 
// type this at the end of the file 

module.exports = router;

  • Related