将 express-validator 与打字稿一起使用时端点没有响应

no response from endpoint when using express-validator with typescript

我正在尝试验证我的端点的响应主体,但是,当我使用 express-validator 时,我没有收到来自 hat 端点的响应。我很确定我关注了官方 documentation:

这是我的终点:

import { body, validationResult } from "express-validator";

export const accountRouter = express.Router();

accountRouter.post( "/signIn", body('userName').notEmpty, body('password').notEmpty, async ( req: express.Request, res: express.Response)  =>  {
    const errors = validationResult(req);
    if (!errors.isEmpty()) return res.sendStatus(400);
    
    ...doStuff();
})

您错过了验证链函数上的括号。这是更正后的路线:

accountRouter.post( "/signIn", body('userName').notEmpty(), body('password').notEmpty(), async ( req: express.Request, res: express.Response)  =>  {
    const errors = validationResult(req);
    if (!errors.isEmpty()) return res.sendStatus(400);
    
    ...doStuff();
})

这是它的文档:https://express-validator.github.io/docs/validation-chain-api.html#notempty