mongoose TypeError: Schema is not a constructor

自闭症网瘾萝莉.ら 提交于 2019-12-03 10:45:49

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

crown

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

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

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