Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose - TypeError: object is not a function

I am trying to export a Mongoose model from my model/user.model.js file to my 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 I declared my schema as UserSchema, however I thought I exported the file with the variable User

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

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

Any ideas? Thanks in advance.

like image 289
Richard Hamilton Avatar asked Aug 05 '15 17:08

Richard Hamilton


1 Answers

You are mistakenly assigning Schema to an instance:

var Schema = mongoose.Schema();

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

var Schema = mongoose.Schema;
like image 151
robertklep Avatar answered Oct 05 '22 23:10

robertklep