如何在我的节点应用程序中使用 if else 语句和表达验证器进行评估

问题描述

我正在尝试使用 express 在邮递员的帮助下对以下一些参数进行验证。现在,邮递员没有给我任何与我的代码相关的回复,所以我什至不知道错误来自哪里。请帮帮我。路由应该接受包含规则和数据字段的 JSON 数据以验证规则。

当condition的值大于或等于condition_value时,下面的json文件应该返回成功或200状态码,当condition小于或等于condition_value时,我应该得到400状态响应或失败,根据参数判断下面当我在邮递员中输入数据类型时。

{
    "rule": {
        "field": "missions","condition": "50","condition_value": 30
    },"data": {
        "name": "James Holden","crew": "Rocinante","age": 34,"position": "Captain","missions": 45
    }
}

这是我的验证代码,其中包含检查条件是否小于或大于等于 condition_value 到 400 响应或 200 响应的逻辑。

router.post(
    "/validate_rule",(req,res) => {
        const SecondSchema = req.body;
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            throw new HttpError("Please kindly fill empty field",400);
        } else if (condition !== condition_value) {
            res.status(400).json({
            message:
                "Error,Condition must be greater than or equal to condition_value",});
        } else if (condition <= condition_value) {
            res.status(400).json({
            message:
                "Error,Condition must be greater than or equal to condition_values",});
        }
        return res.status(200).json({
            message: "Success",});
    }
);

解决方法

我刚刚意识到我做了两次 req 和 response,第一次是在验证码之前,然后是之后。改正之后,我还在努力弄明白逻辑或者if else语句。