mongoose TypeError: Schema is not a constructor
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);
It should be Schema.Types.ObjectId
, not Schema.ObjectId
: http://mongoosejs.com/docs/schematypes.html
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);
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);