简体   繁体   English

类型错误:用户不是构造函数

[英]TypeError: User is not a constructor

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.我正在尝试使用 post 请求将用户保存到 mongodb 数据库,如下所示,但出现错误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猫鼬 4.8.6
express 4.15.2快递 4.15.2
node 6.6节点 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');
    });
});

在此处输入图片说明

You need to create model from your UserSchema and then export it, then you can create new User objects.您需要从您的UserSchema创建model ,然后将其导出,然后您就可以创建新的 User 对象。

// 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)
// 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:将小写的user变量更改为大写(如下所示)奇怪地工作:

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

I really thought this was just a best practice but eventually, seems to be mandatory.我真的认为这只是一种最佳做法,但最终似乎是强制性的。

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在我的模型/用户中,我写了module.export ,最后省略了s
When you run the code it then gives you that error当你运行代码时,它会给你那个错误
I would advice checking on your module.exports = mongoose.model('User', UserSchema) spellings我建议检查你的module.exports = mongoose.model('User', UserSchema)拼写

Change this var user = new User();更改此var user = new User(); for this var User = new User();对于这个var User = new User();

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM