Rhys Edwards Rhys Edwards - 1 year ago 357
Node.js Question

Mongoose TypeError: User is not a constructor

I'm trying to add a subdocument to a parent schema with Mongoose and MongoDB however I'm being thrown the following error:

TypeError: User is not a constructor

This is based off Mongoose's documentation on subdocuments and I think everything is the same. How can I debug this further?


// Add a destination to the DB
router.post('/add', function(req, res, next) {
let airport = req.body.destination
let month = req.body.month
let id = (req.user.id)

User.findById(id , function (err, User) {
if (err) return handleError(err)

function addToCart (airport, month, id) {
var user = new User ({
destinations: [(
airport = '',
month = ''

dog.destinations[0].airport = airport
dog.destinations[0].month = month


var destinationSchema = new Schema({
airport: String,
month: String

// Define the scheme
var User = new Schema ({
firstName: {
type: String,
index: true
lastName: {
type: String,
index: true
email: {
type: String,
index: true
homeAirport: {
type: String,
index: true
destinations: [destinationSchema]


module.exports = mongoose.model('User', User)

Answer Source

JavaScript is case sensitive about the variable names. You have User model and the User result with the same name.

Your code will work with the following change :

   User.findById(id , function (err, user) {
/*                                   ^ use small `u` */
       if (err) return handleError(err)

/* rest of your code */

Also keep in mind that further in your code you are declaring another variable named user. You will need to change that to something different.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download