AndrewKeig / express-validation

express-validation is an express middleware that validates a request and returns a response with errors; if any of the configured validation rules fail.
MIT License
393 stars 85 forks source link

error: TypeError: external_express_validation_default(...) is not a function #142

Closed tytom2003 closed 2 years ago

tytom2003 commented 3 years ago

My program show below error. How to solve the error? Thank you. .post(external_express_validation_default()(articlevalidation), article_controller.articlePost); TypeError: external_express_validation_default(...) is not a function

article.route.js import express from 'express'; import validate from 'express-validation'; import articleCtrl from '../controllers/article.controller'; import paramValidation from '../../config/param-validation'; const router = express.Router(); router.route('/') .get(articleCtrl.articleGet) .post(validate(paramValidation.createArticle), articleCtrl.articlePost); router.route('/:article_id') .put(articleCtrl.articlePut) .delete(articleCtrl.articleDelete); export default router;

param-validation.js import Joi from 'joi'; export default { // POST /api/article createArticle: { body: { user_id: Joi.number().required(), article_title: Joi.string().required(), article_tag: Joi.string().required(), article_content: Joi.string().min(20).required() } }, // POST /api/user createUser: { body: { user_name: Joi.string().required(), user_mail: Joi.string().email().trim().required(), user_password: Joi.string().regex(/[a-zA-Z0-9]{6,30}$/).required() } } };

papandreou commented 2 years ago

Sorry, this report is a bit of a mess. You should always try to provide a minimal reproduction. This spans multiple files, one of which isn't included (article.controller), and it's pasted as text so it's pretty difficult to understand.

I believe the problem is that express-validation does not use named exports, so import validate from 'express-validation' won't work. Try this instead:

import { validate } from 'express-validation';