mongoose TypeError: Schema is not a constructor
Asked Answered
O

18

23

I've encountered a strange thing. I have several mongoose models - and in one of them (only in one!) I get this error:

TypeError: Schema is not a constructor

I find it very strange as I have several working schemas. I tried logging mongoose.Schema in the non-working schema and it is indeed different from the mongoose.Schema in my working schemas - how is that possible? The code is almost identical. Here's the code for the non-working schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var errSchema = new Schema({
  name: String,
  images:[{
    type:String
  }],
  sizes:[{
    type: String
  }],
  colors:[{
    type: Schema.ObjectId,
    ref: 'Color'
  }],
  frontColors:[{
    type: Schema.ObjectId,
    ref: 'Color'
  }],
  script: Boolean
},{
  timestamps: true
});

var Err = mongoose.model('Err', errSchema);

module.exports = Err;

Code for a working schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var colorSchema = new Schema({
  name: String,
  image: String,
  rgb: String,
  comment: String,
});

var Color = mongoose.model('Color', colorSchema);

module.exports = Color;

Any help would be appreciated!

Ob answered 27/10, 2016 at 9:20 Comment(3)
Of course!! Man, do I feel stupid! Thank you for your swift reply. Make an answer and I'll accept it :)Revivify
i am getting the same error, how did u resolve this ?Polypody
@Polypody - my problem came from a missing Types in Schema.Types.ObjectId. Once I added this, my problem disappeared.Revivify
E
5

It should be Schema.Types.ObjectId, not Schema.ObjectId: http://mongoosejs.com/docs/schematypes.html

Enteric answered 27/10, 2016 at 9:30 Comment(0)
G
19

I have encountered the same thing. I have previous code like this

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema();
    var schema = new Schema({
        path : {type:string , required:true},
        title: {type:string , required: true}
    })
 module.export = mongoose.model('game', schema);

Then I solved the constructor problem using below script

   var mongoose = require('mongoose');
    var schema = mongoose.Schema({
        path : {type:string , required:true},
        title: {type:string , required: true}
    })
 module.export = mongoose.model('game', schema);
Git answered 5/7, 2017 at 4:19 Comment(1)
So, this technique worked for me also...but why? The official docs clearly show it in use as a constructor with new keyword. And, that is with simple data - not like above with the 'Objects stuff.' mongoosejs.com/docs/guide.htmlEchols
S
14

The problem in my case was the export which should (s) at the end:

Problem: missing (s) in exports

module.export = mongoose.model('Event', EventSchema);

Solution: add (s) to exports

module.exports = mongoose.model('Event', EventSchema);
Shortage answered 1/12, 2020 at 6:24 Comment(0)
E
5

It should be Schema.Types.ObjectId, not Schema.ObjectId: http://mongoosejs.com/docs/schematypes.html

Enteric answered 27/10, 2016 at 9:30 Comment(0)
J
5

I've solved this problem by importing Schema with upper case.

Previous:

const Scheme = mongoose.schema;

After Fixing:

const Schema = mongoose.Schema;

Full Schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ItemSchema = new Schema({
    name : {
        type: String,
        required : true
    },
    date : {
        type : Date,
        default : Date.Now
    }
});
module.exports = mongoose.model('Item', ItemSchema);
Jacalynjacamar answered 6/8, 2019 at 11:33 Comment(0)
G
2

Understand am late to the Party, but the below code worked for me, could be helpful for someone using mongoose version 5.2.15

const mongoose = require('mongoose');
const Scheme = mongoose.Schema;

const ItemSchema = new Scheme({
    name: {
        type: String,
        require: true
    },
    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = Item = mongoose.model('Item', ItemSchema);
Gisellegish answered 18/9, 2018 at 1:24 Comment(0)
T
1

Use mongoose.Schema instead of let Schema = mongoose.Schema();

Twotone answered 10/6, 2020 at 5:16 Comment(0)
T
1

I had the same issue

previous code

module.export = mongoose.model('AllClassList', allClassListSchema);

correct code

module.exports = mongoose.model('AllClassList', allClassListSchema);

I was missing the "s" at the end of export

Toxicogenic answered 21/11, 2020 at 14:58 Comment(0)
S
1

For anyone who still has the error

  1. Check your mongoose version on your package.json to see whether it's an older version
  2. Re install it to a latest version

Worked for me!!

Schnozzle answered 3/2, 2021 at 7:27 Comment(0)
B
1

I encounter this my solution is adding (s) in module.export, I forgot to add s in the end that's why it throw me that my Schema is not a constructor.

Benzedrine answered 13/3, 2021 at 13:24 Comment(0)
S
1

I have faced the issue My previous code is like this :

const Mongoose = require('mongoose')
const Schema = Mongoose.Schema()  // here making Schema() like this is the issue

fixed code:

const Schema = Mongoose.Schema

   `

    const placeSchema` = new Schema({
        title: { type: String, required: true },
        description: { type: String, required: true },
        image: { type: String, required: true },
        address: { type: String, required: true }

module.exports = Mongoose.model('Place' , placeSchema)
Stranger answered 24/9, 2022 at 15:38 Comment(0)
T
0
import mongoose from "mongoose";

connectDB(mongoURI); // database connection

// create schema 

    const student = new mongoose.Schema({
      name: String,
      workout: Boolean,
      height: Number,
    });
    
   // create model in students collections

    const Student = new mongoose.model("Student", student);
  
     // store data
    const adder = async () => {
   
      const ss = await Student.create({
        name: "jiya",
        workout: true,
        height: 6,
      });
    
    };
    

    //call function
    adder();
Tennyson answered 5/1, 2022 at 12:43 Comment(1)
While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Louvre
D
0

I have tried this one with the help of _Schema

const mongoose = require('mongoose');
const _schema  = mongoose.Schema;
//creat schema
const IdeaSchema = new _schema({
    title: {
        type: String,
        required: true
    },
    details: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    }
});
module.exports = mongoose.model('ideas', IdeaSchema);
Demirelief answered 4/3, 2022 at 18:54 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Unlettered
K
0

This works for me

const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema(
  {
    username: {
      type: String,
      required: true,
      unique: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
    password: {
      type: String,
      required: true,
    },
    profilePic: {
      type: String,
      default: "",
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("User", UserSchema);
Kolo answered 28/8, 2022 at 9:19 Comment(0)
P
0

const Mongoose = require('mongoose') const User = Mongoose.Schema({name: String}) worked for me. replacing const mongoose with Mongoose

Pettis answered 2/10, 2022 at 10:12 Comment(0)
L
0

This may seem trivial, but npm uninstalling and then reinstalling Mongoose worked for me.

Lazulite answered 25/11, 2022 at 9:48 Comment(0)
P
0

For me this error occurred due to wrong import in server.js file -

My mongoose.js file -

    const member = mongoose.Schema({
    id: Number,
    memberId: Number,
    name: String,
    contact: String,
    doj: String,
    plan: String,
    fees: Number
});

const Members = mongoose.model("Member", member);

const user = mongoose.Schema({
    email: String,
    password: String
});

const User = mongoose.model("User", user);

module.exports = Members;
module.exports = User;

And my server.js file import was -

const {Members, User} = require('./mongoose');

Fixed import -

const Members = require('./mongoose');
const User = require('./mongoose');
Panchromatic answered 28/3, 2023 at 21:41 Comment(0)
C
0

For me it worked by changing:

module.exports = { blog };

To:

module.exports = blog;
Crevasse answered 16/2 at 12:43 Comment(0)
C
-1

the problem was neither capitalization Schama nor forgetting "s" for export. it was accidentally deleting: "('mongoose')" while I was codding!!!so I added it again! Problem solved!!

const mongoose = require('mongoose');
Cryometer answered 14/7, 2021 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.