5

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.

1 Answer 1

11

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;
Sign up to request clarification or add additional context in comments.

2 Comments

@Raz time to post a new question then :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.