Mongoose TypeError: schema is not a constructor

I came across something strange. I have several mongoose models - and in one of them (only one!) I get this error:

TypeError: Schema is not a constructor

It’s very strange for me, because I have several workflows. I tried writing mongoose.Schemato a non-working scheme and it really differs from mongoose.Schema in my working schemes - how is this possible? The code is almost identical. Here is the code for the inoperative circuit:

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 workflow:

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!

+4
source share
2 answers

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

+2

. ,

    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);

, 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);
+9

Source: https://habr.com/ru/post/1659040/


All Articles