TypeError: UserSchema is not a constructor (Schema is not a constructor MongoDB)

1291 views javascript

Not sure why this is happenening, tried removing all instances of new, switching to let from const ect. Can run site however when I run a post request via a html form, get an error on the line "const user = new UserSchema" ect. of TypeError: UserSchema is not a constructor.

const express = require('express');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bodyParser = require('body-parser');

let app = express();
let mongoDB = //hiding url for obvious reasons

mongoose.connect(mongoDB, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
const UserSchema = new Schema({
    name: String,
    email: String

app.set('view engine', 'ejs');
app.use('/assets', express.static('assets'));
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', function(req,res){

app.post('/', function (req, res) {
    const user = new UserSchema(req.body.name, req.body.email);
    .then(item => {
      res.send("item saved to database");
    .catch(err => {
      res.status(400).send("unable to save to database");


answered question

1 Answer


You have to assign the schema as a mongoose model.

var User= mongoose.model('user', UserSchema);

posted this

Have an answer?


Please login first before posting an answer.