'Mongoose Schema cannot be identified by code

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//broken schema
var coursesSchema = new Schema({
    college: {
        type: String,
        required: true
    },
    units: {
        type: Number,
        required: true
    },
    course_code: {
        type: String,
        required: true
    },
    course_name: {
        type: String,
        required: true
    },
    class_number: {
        type: String,
        required: true
    },
    section: {
        type: String,
        required: true
    },
    class_day: {
        type: String,
        required: true
    },
    time_start: {
        type: String,
        required: true
    },
    time_end: {
        type: String,
        required: true
    },
    faculty: {
        type: String,
        required: true
    }
});


module.exports = mongoose.model('Courses', coursesSchema);

//working schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var loaSchema = new Schema({
    process_name: {
        type: String,
        required: true
    },
   process_details: {
       type: String,
       required: true
    },
    process_deadline: {
        type: String,
        required: true
    },
});


module.exports = mongoose.model('LOA', loaSchema);

I have this schema which I export into index.js file. This schema works in other parts of the code except in index.js. When I query Courses schema, it returns model.find() is not a function. When I try creating a new Courses object it says Constructor is not a schema. I have other schemas that work fine inside index.js and I get to query them, but this schema is an exception.

Does anyone know the reason behind this?



Solution 1:[1]

index.js is the special file and actually it's root of your module, it's better separate the schema from that to prevent from bugs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evan