Mongoose and new Schema: returns "ReferenceError: Schema is not defined"

Exactly, in your Service.js, what is Schema? You don't have an object named Schema.

...
var serialSchema = new Schema({
                       ^^^^^^

change it to mongoose.Schema then it will be fine.


This can be occurred due to several reasons. first one is that you might forgotten to import Schema.You can fix that as follows.

const Schema = mongoose.Schema;

const serialSchema = new Schema({
   serial: {type: String},
   game: {type: String},
   date: {type: Date, default: Date.now},
});

Sometimes you have forgotten to import your newly created model.This type of errors can be solve by importing created model to your working file.

const serialModel = mongoose.model('serials', serialSchema);

In my case, I was referencing a schema inside of another schema and had to replace type: Schema.Types.ObjectId with mongoose.Types.ObjectId.


you forgot to define the Schema like this on you second line

var Schema = mongoose.Schema