admin管理员组

文章数量:1298125

I am trying to save a user to mongodb database using post request as follow, but I got the error TypeError: User is not a function. It's a pretty simple set up of the code but i can't figure out anything wrong with it.

I am using:
mongoose 4.8.6
express 4.15.2
node 6.6

// models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    email: {
        type: String,
        unique: true,
        lowercase: true
    },
    password: String
});

// server.js
var User = require('./models/user');

app.post('/create-user', function(req, res, next) {
    var user = new User(); // TypeError: User is not a constructor
    user.email = req.body.email;
    user.password = req.body.password;

    user.save(function(err) {
        if (err) return next(err);
        res.json('Successfully register a new user');
    });
});

I am trying to save a user to mongodb database using post request as follow, but I got the error TypeError: User is not a function. It's a pretty simple set up of the code but i can't figure out anything wrong with it.

I am using:
mongoose 4.8.6
express 4.15.2
node 6.6

// models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    email: {
        type: String,
        unique: true,
        lowercase: true
    },
    password: String
});

// server.js
var User = require('./models/user');

app.post('/create-user', function(req, res, next) {
    var user = new User(); // TypeError: User is not a constructor
    user.email = req.body.email;
    user.password = req.body.password;

    user.save(function(err) {
        if (err) return next(err);
        res.json('Successfully register a new user');
    });
});

Share Improve this question asked Mar 12, 2017 at 13:00 eulercodeeulercode 1,1574 gold badges17 silver badges32 bronze badges 1
  • It would be better if you've shared User module as the problem is related to that. – Mert Akcakaya Commented Mar 12, 2017 at 13:04
Add a comment  | 

7 Answers 7

Reset to default 14

You need to create model from your UserSchema and then export it, then you can create new User objects.

// models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    email: {
        type: String,
        unique: true,
        lowercase: true
    },
    password: String
});

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

You got that error because you exported the modules wrongly,
In my case in the models/user I had written module.export leaving out the s at the end
When you run the code it then gives you that error
I would advice checking on your module.exports = mongoose.model('User', UserSchema) spellings

// models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
    email: {
        type: String,
        unique: true,
        lowercase: true
},
    password: String
});

var User = mongoose.model('User', UserSchema)
module.exports =  { User } <-- capital 'U'

Changing the lowercase user variable into uppercase (like below) strangely worked:

const User = mongoose.model('users');

I really thought this was just a best practice but eventually, seems to be mandatory.

I was also facing the same error but this worked for me.

I did change my export file

var User = mongoose.model('User',userSchema);
module.exports = {User};

To

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

Change this var user = new User(); for this var User = new User();

I also got struck in the same issue but I had no mistake in exporting file and remaining all codes were fine as well.

The only problem with my code was I had imported the model as below:

const { User } = require('./Database/Models/UserModel');

Since I had only one model exported from the file UserModel.js, I should not use {} while importing the model.

Correct way of importing would be:

const User = require('./Database/Models/UserModel');

And my issue got resolved.

本文标签: javascriptTypeError User is not a constructorStack Overflow