为什么我不断收到TypeError:用户不是构造函数?

时间:2019-06-25 02:33:03

标签: javascript node.js express mongoose

我正在练习创建模型和路线,并正在使用邮递员发送POST请求以对其进行测试。但是,我不断得到的用户不是构造函数错误。

index.js(路由)

const express = require('express')
require('./db/mongoose')
const User = ('./models/user')

const app = express()
const port = process.env.PORT || 3000

app.use(express.json())

app.post('/users', (req, res) => {
    const user = new User(req.body)

    user.save().then(() => {
        res.send(user)
    }).catch(() => {

    })
})

app.listen(port, () => {
    console.log(port + ' is aliiiiiiiive!')
})

用户(模式)

const mongoose = require('mongoose')
const validator = require('validator')

const User = mongoose.model('User', { 
    name: {
        type: String,
        required: true, 
        trim: true
    }, 
    email: {
        type: String,
        require: true,
        trim: true, 
        lowercase: true,
        validate(value) {
            if(!validator.isEmail(value)) {
                throw new Error('Email is invalid')
            }
        }
    },
    age: {
        type: Number,
        default: 0,
        validate(value) {
            if(value < 0) {
                throw new Error('Age must be a positive number.')
            }
        }
    },
    password: {
        type: String,
        trim: true,
        lowercase: true,
        required: true,
        minlength: 7,
        validate(value) {
            if( value.toLowerCase().includes("password")) {
                throw new Error("Password can't be 'password'.")
            }
        }
    }
})

module.exports = User

mongoose.js

const mongoose = require('mongoose')

mongoose.connect('mongodb://127.0.0.1:27017/task-manager-api', {
    useNewUrlParser: true,
    useCreateIndex: true
})

我希望它发送回带有邮递员发送的以下信息的对象:

{
    "name": "Michael",
    "email": "email@eail.com",
    "password": "ThisIsAPassword"
}

2 个答案:

答案 0 :(得分:1)

您必须在编译模型之前定义一个userSchema,如下所示:

const mongoose = require('mongoose')
const validator = require('validator')

const userSchema = new mongoose.Schema({ 
    name: {
        type: String,
        required: true, 
        trim: true
    }, 
    email: {
        type: String,
        require: true,
        trim: true, 
        lowercase: true,
        validate(value) {
            if(!validator.isEmail(value)) {
                throw new Error('Email is invalid')
            }
        }
    },
    age: {
        type: Number,
        default: 0,
        validate(value) {
            if(value < 0) {
                throw new Error('Age must be a positive number.')
            }
        }
    },
    password: {
        type: String,
        trim: true,
        lowercase: true,
        required: true,
        minlength: 7,
        validate(value) {
            if( value.toLowerCase().includes("password")) {
                throw new Error("Password can't be 'password'.")
            }
        }
    }
})

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

现在它是一个构造函数,因为我们说User的每个实例都是new的{​​{1}}实例。

答案 1 :(得分:0)

我知道了。在index.js文件的第三部分,我省略了require。

代替此:

const User = ('./models/user')

应该是这样的:

const User = require('./models/user')

谢谢大家的帮助!