[英]Unable to find where this middleware error is coming from
我得到一个中间件不是 function 错误,我不知道为什么......
[Error] TypeError: middleware is not a function
路线 JS
import express from 'express'
import userController from '../../../server/src/controllers/user.controller.js'
import awaitHandlerFactory from '../../../server/src/middleware/awaitHandlerFactory.middleware.js'
export const router = express.Router();
import {auth} from '../middleware/auth.middleware.js'
import { createUserSchema, updateUserSchema, validateLogin } from '../middleware/validators/userValidator.middleware.js'
router.get('/get', auth(), awaitHandlerFactory(userController.getAllUsers))
router.post('/register', createUserSchema, awaitHandlerFactory(userController.createUser))
router.post('/login', validateLogin, awaitHandlerFactory(userController.userLogin))
等待处理程序。
export const awaitHandlerFactory = (middleware) => {
return async(req, res, next) => {
try {
await middleware(req, res, next)
}
catch(err)
{
next(err)
}
}
}
router.post('login') 路由不起作用,其他两个路由都可以正常工作。
这是来自 controller 模块。
userLogin = async(req, res, next) => {
this.checkValidation(req)
const { email, password: pass} = req.body
const user = await UserModel.findOne({ email })
if(!user)
{
throw new HttpException(401, 'Unable to login.')
}
const isMatch = await bcrypt.compare(pass, user.password)
if (!isMatch) {
throw new HttpException(401, 'Incorrect password!')
}
const secretKey = process.env.SECRET_JWT || ""
const token = jwt.sign({ user_id: user.id.toString() }, secretKey, {
expiresIn: '24h'
})
const { password, ...userWithoutPassword } = user
res.send({ ...userWithoutPassword, token})
}
这是 model 模块
findOne = async (params) => {
const {columnSet, values} = multipleColumnSet(params)
const sql = `SELECT * FROM ${this.tableName} WHERE ${columnSet}`
const result = await query(sql, [...values])
return result[0]
}
验证登录
import { body } from 'express-validator';
import { Role } from '../../utility/userRoles.utility.js';
export const validateLogin = [
body('email')
.exists()
.withMessage('Email is required')
.isEmail()
.withMessage('Must be a valid email')
.normalizeEmail(),
body('password')
.exists()
.withMessage('Password is required')
.notEmpty()
.withMessage('Password must be filled')
];
我什至无法指出错误的来源,这是错误的全部内容。
[Error] TypeError: middleware is not a function
express:router dispatching POST /api/login +3s
express:router query : /api/login +1ms
express:router expressInit : /api/login +2ms
express:router jsonParser : /api/login +1ms
express:router jsonParser : /api/login +1ms
express:router urlencodedParser : /api/login +1ms
express:router corsMiddleware : /api/login +9ms
express:router trim prefix (/api) from url /api/login +1ms
express:router router /api : /api/login +0ms
express:router dispatching POST /login +1ms
express:router errorMiddleware : /api/login +1ms
[Error] TypeError: middleware is not a function
我想通了......几个小时后。 基本上我重写了代码,因为我第一次做了一个凌乱的版本。 出于某种原因,VS-Code 自动将模块从一个完全不同的文件夹链接起来。
根据 ../ 的数量,它会从当前目录进入另一个目录。 这就是为什么我不能使用 userLogin function 因为我在其他版本上没有那么远......
import userController from '../../../server/src/controllers/user.controller.js'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.