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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With