Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

javascript - Node.js-定义了一个函数却抛出错误(Node.js - Defined a function yet throws an error)

I am working on a chat app.

(我正在使用聊天应用程序。)

I was using express-validator.

(我正在使用快递验证器。)

const express = require('express');
const validator = require('express-validator');

const container = require('./container');


function ConfigureExpress(app){
        app.use(express.static('public'));
        app.use(cookieParser());
        app.set('view engine', 'ejs');
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({extended: true}));

        app.use(validator());

        app.use(session({
            secret: 'thisisasecretkey',
            resave: true,
            saveInitialized: true,
            store: new MongoStore({mongooseConnection: mongoose.connection})
        }));

        app.use(flash());

        app.use(passport.initialize());
        app.use(passport.session());
    }

This is the error getting thrown when I run nodemon server:

(这是我运行nodemon服务器时抛出的错误:)

C:UsersUtkarsh Raiaqchatserver.js:49
        app.use(validator());
                ^

TypeError: validator is not a function

I tried to resolve the issue, but am not able to understand where am I going wrong.

(我试图解决该问题,但无法理解我要去哪里。)

I tried searching for this issue.

(我尝试搜索此问题。)

  ask by Kaneki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the docs :

(从文档 :)

// ...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));
});

So instead you'll want to create your own validation method like:

(因此,您需要创建自己的验证方法,例如:)

function ConfigureExpress(app){
    app.use(express.static('public'));
    app.use(cookieParser());
    app.set('view engine', 'ejs');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));

    //Our validator code
    app.use([
        /*
        ex. validator.check('username').isAlphanumeric()
        */
    ]);

    app.use(session({
        secret: 'thisisasecretkey',
        resave: true,
        saveInitialized: true,
        store: new MongoStore({mongooseConnection: mongoose.connection})
    }));

    app.use(flash());

    app.use(passport.initialize());
    app.use(passport.session());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...