Mongoose - TypeError: object is not a function

I am trying to export a Mongoose model from my model/user.model.js file to the server.js file in my server directory.

model /user.model.js

 var mongoose = require('mongoose'); var Schema = mongoose.Schema(); var UserSchema = new Schema({ instagramId: { type: String, index: true }, email: { type: String, unique: true, lowercase: true }, password: { type: String, select: false }, userName: String, fullName: String, picture: String, accessToken: String }); module.exports = mongoose.model('User', UserSchema, 'users'); 

server.js

 var User = require('./models/user.model'); mongoose.connect(config.db); 

I get this error message

\ server \ models \ user.model.js 5

var UserSchema = new schema ({

TypeError: object is not a function

I know that I declared my schema as UserSchema , however I thought I exported the file with the User variable

 module.exports = mongoose.model('User', UserSchema, 'users'); 

I am trying to use the User name to query my mongoose model.

Any ideas? Thanks in advance.

+5
source share
1 answer

You erroneously assign Schema instance:

 var Schema = mongoose.Schema(); 

Instead, it should be assigned the mongoose.Schema class:

 var Schema = mongoose.Schema; 
+10
source

All Articles