简体   繁体   中英

Why do I keep getting TypeError: User is not a constructor?

I'm practicing creating models and routes and am using postman to send a POST request to test it out. However, I keep getting the user is not a constructor error.

index.js (route)

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!')
})

User (schema)

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

I expect it to send back an object with the following information I'm sending on Postman:

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

You have to define a userSchema before compiling the model, like this:

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

Now it is a constructor, because we are saying each instance of User is a new instance of userSchema .

I figured it out. On the third in my index.js file, I left out require.

Instead of this:

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

It should have been this:

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

Thanks for all your help, everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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