mongoose TypeError: Schema is not a constructor

拜拜、爱过 提交于 2019-12-21 03:32:20

问题


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!


回答1:


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




回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/40280783/mongoose-typeerror-schema-is-not-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!