我不断收到UnhandledPromiseRejectionWarning:TypeError:validate不是函数错误

时间:2020-06-01 23:15:52

标签: javascript node.js express

当我尝试运行此代码时,我得到了UnhandledPromiseRejectionWarning:TypeError:validate不是函数错误,据我所知,我已经尝试了所有方法,但似乎没有任何效果。这是我的代码 我的模特

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

function validate(user){
    const schema = {
        firstname: Joi.string().min(3).max(30).required(),
        lastname: Joi.string().min(3).max(30).required(),
        email: Joi.string().email({ minDomainAtoms: 2 }),
        password: Joi.string().min(6).regex(/^[a-zA-Z0-9]{3,30}$/)

    };
    return Joi.validate(user, schema);
}

module.exports = userSchema;
module.exports = User;
module.exports = validate;

这是validate函数的实现

const {User, validate} = require('../models/user');
const express = require('express');
const router = express();
const _ = require('lodash');

router.get('/test', (req, res) => {
    console.log(module);
});


router.post('/register', async (req, res) => {
    const { error } = validate(req.body);
    if(error) return res.status(400).send(error.details[0].message);

    let user = await User.findOne({ email: req.body.email });
    if(user) return res.status(400).send('User already exists');

    user = await new User({
        name: {
            first: req.body.first,
            last: req.body.last
        },
        email: req.body.email,
        password: req.body.password,
        location: {
            ipAddress: req.ip
        }
    });
    try{
        await user.save();
        res.send(_.pick(user, ['_id', 'name.first', 'name.last', 'email']));
    }catch(ex){
        res.status(400).send(ex.details[0].message);
    }

});

我尝试通过运行console.log(module)进行调试,并且可以看到该函数已导入。

0 个答案:

没有答案