app.use(validator())TypeError:验证器不是函数

时间:2019-07-05 10:05:29

标签: node.js express

大家好,我试图在我的应用程序中安装express-validator,但是当我试图要求该模块并在app.use()函数中使用它时,我面临着> python parser.py "csv.txt" "map.txt" CSV File: csv.txt Mapping File: map.txt Converting: man -> nut Adding: 1 -> 2 Converting: man -> bag Adding: 1 -> 3 Converting: nut -> bag Adding: 2 -> 3 Converting: rat -> cat Adding: 4 -> 5 Converting: dog -> fog Adding: 6 -> 7 Converting: dog -> cat Adding: 6 -> 5 Converting: dog -> man Adding: 6 -> 1 Converting: fog -> cat Adding: 7 -> 5 Converting: fog -> man Adding: 7 -> 1 Converting: cat -> man Adding: 5 -> 1 FINAL MAPPING: [1, 2] [1, 3] [2, 3] [4, 5] [6, 7] [6, 5] [6, 1] [7, 5] [7, 1] [5, 1]

TypeError: validator is not a function代码

app.js

1 个答案:

答案 0 :(得分:2)

require("express-validator")不是中间件。中间件是:

check([field, message])

body([fields, message])

oneOf(validationChains[, message]) ..etc..

取自doc的基本示例:

// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');

app.post('/user', [
  // username must be an email
  check('username').isEmail(),
  // password must be at least 5 chars long
  check('password').isLength({ min: 5 })
], (req, res) => {
  // Finds the validation errors in this request and wraps them in an object with handy functions
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }

  User.create({
    username: req.body.username,
    password: req.body.password
  }).then(user => res.json(user));
});