TypeError: User is not a constructor

前端 未结 5 1566
长情又很酷
长情又很酷 2020-12-21 16:40

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

5条回答
  •  滥情空心
    2020-12-21 17:25

    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)
    

提交回复
热议问题