Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose TypeError: Schema is not a constructor

Tags:

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!

like image 847
David Stenstrøm Avatar asked Oct 27 '16 09:10

David Stenstrøm


2 Answers

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); 
like image 135
crown Avatar answered Oct 23 '22 12:10

crown


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); 
like image 27
DiaMaBo Avatar answered Oct 23 '22 14:10

DiaMaBo