Home > other >  TypeError: mongoose.Schema is not a constructor. Not working removing "new" > TypeError
TypeError: mongoose.Schema is not a constructor. Not working removing "new" > TypeError

Time:08-18

I am trying to figure out what's going on in this code. I'm following this tutorial: https://www.tutsmake.com/login-and-registration-form-in-node-js-express-mongodb/#comment-2355 . Its an expressJS app and I'm trying to connect this app with mongodb to register users. This is the userModel used to create schema:

const mongoose = require("../database");

 
// create an schema
var userSchema = new mongoose.Schema({
            name: String,
            password: String,
            email:String
        });
 
var userModel=mongoose.model('users',userSchema);
 
module.exports = mongoose.model("Users", userModel);

and when I run the "npm start" I get this: vscode output image

And if I remove the "new", I get this: vscode second output image

CodePudding user response:

You should import mongoose with:

const mongoose = require("mongoose");

Also, define your model just once:

var userSchema = mongoose.Schema({
  name: String,
  password: String,
  email:String
});

module.exports = mongoose.model("Users", userSchema);
  • Related