节点-TypeError:验证器不是函数

时间:2019-06-28 16:23:10

标签: javascript node.js express

我是Node的新手,正在从事一个项目。我被要求使用Node.js创建一个登录页面,安装Express-validator并在entrypoint.js文件中输入我想输入的代码后,每次运行entrypoint.js时都会打印出错误。

TypeError: validator is not a function
at Object.<anonymous> (E:\BookBasket\entrypoint.js:28:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

这是entrypoint.js的代码,

const express = require("express");
const bodyParser = require("body-parser");
const expressHbs = require ('express-handlebars');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require("passport");
const flash = require("connect-flash");
const validator = require("express-validator");

//Using routes for Routing the pages and API
const routes = require('./routes');
const indexRouter = require('./routes/index');

  //Creating database connection seperately for the development and production server
require('./config/config');
//Passport
require('./config/passport');
//Connect to the database
require('./database/database');

var app = express();

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(validator());
app.use(cookieParser());
app.use(session({secret: 'mysupersecret', resave: false, saveUninitialized: false}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
//Static Folder Setup like css, javascript

 app.use('/public', function(req, res, next) {
  console.log(req.url);
  next();
 });

app.use('/public', express.static('public'));

//End of static folder setup

另一件事:-

我是通过npm install安装express-validator

version-“ express-validator”:“ ^ 6.1.0”,

下面是我的package.json代码

"dependencies": {
"bcrypt": "^3.0.6",
"bcryptjs": "^2.4.3",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.4.4",
"csurf": "^1.10.0",
"debug": "~2.6.9",
"express": "^4.16.4",
"express-handlebars": "^3.1.0",
"express-session": "^1.16.2",
"express-validator": "^6.1.0",
"hbs": "~4.0.4",
"http-errors": "~1.6.3",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.6.0",
"morgan": "~1.9.1",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"validator": "^11.0.0"

1 个答案:

答案 0 :(得分:1)

这都是关于版本不匹配的!!

我已经提到我安装了

version - "express-validator": "^6.1.0",

由于版本不匹配,每次我运行entrypoint.js时都会打印出错误。

TypeError: validator is not a function

我已经安装了早期版本的express-validator,那是

"express-validator": "^3.1.2",

然后,它开始正常运行而不会显示任何错误

**

  • 通过以下命令安装Express-validator的早期版本:-

**

npm install --save express-validator@3.1.2