根据条件语句结束express.js请求

时间:2022-09-10 19:16:10

I am using the Model.findOne method from mongoose to find and add some new data into a document. What kind of data is added is based on conditional if statements. See below:

我正在使用mongoose的Model.findOne方法来查找并向文档中添加一些新数据。添加了哪种数据基于条件if语句。见下文:

companyTotal.findOne({companyName: "xyz"}, function (err, doc) {
        if (err) {
            sendJsonResponse(res, 400, err)
        } else if (doc) {

                if (req.body.q1 === "poor") {
                    doc.poor += 1;
                } else if (req.body.q1 === "okay") {
                    doc.okay += 1;
                } else if (req.body.q1 === "well") {
                    doc.well += 1;
                } else if (req.body.q1 === "very well") {
                    doc.veryWell += 1;
                } else {
                    sendJsonResponse(res, 401, {"message": "Wrong data entry."})
                }
        }
        doc.save(function (err, data) {
            if (err) {
                sendJsonResponse(res, 400, err)
            } else {
                sendJsonResponse(res, 200, data);
            }
        });

    });

If none of the conditional statements are met I want to end the request and send an error message back. I am using the sendJsonResponse function to handle any kind responses that need to be sent to the user along with the status codes.

如果没有满足任何条件语句,我想结束请求并发回错误消息。我正在使用sendJsonResponse函数来处理需要与状态代码一起发送给用户的任何类型的响应。

function sendJsonResponse(res, status, content) {
  res.status(status);
  res.json(content);
}

Based on my knowledge res.json calls res.end(); which then ends the request. But everytime the condition is not met it displays the error message BUT still runs the doc.save() method and adds the data into the document.

根据我的知识,res.json调用res.end();然后结束请求。但是每次不满足条件时它都会显示错误消息但仍然运行doc.save()方法并将数据添加到文档中。

What am I doing wrong? How can I end a request within if statements if conditions are not met?

我究竟做错了什么?如果条件不满足,如何在if语句中结束请求?

2 个解决方案

#1


0  

define flag variable as

将flag变量定义为

flag=0 ; 

Then

else {
        flag++;
        sendJsonResponse(res, 401, {"message": "Wrong data entry."})
      }

Then

if (flag ==0 ){  
doc.save(function (err, data) {
            if (err) {
                sendJsonResponse(res, 400, err)
            } else {
                sendJsonResponse(res, 200, data);
            }
        });
 } 

#2


0  

In my opinion it should look like this:

在我看来,它应该是这样的:

function sendJsonResponse(res, status, content) {
  return res.status(status).json(content); // you can send response only one 
}

and:

companyTotal.findOne({companyName: "xyz"}, function (err, doc) {
    if (err) {
        return sendJsonResponse(res, 400, err);
    } else if (doc) {
            if (req.body.q1 === "poor") {
                doc.poor += 1;
            } else if (req.body.q1 === "okay") {
                doc.okay += 1;
            } else if (req.body.q1 === "well") {
                doc.well += 1;
            } else if (req.body.q1 === "very well") {
                doc.veryWell += 1;
            } else {
                return sendJsonResponse(res, 401, {"message": "Wrong data entry."});
            }
    }
    doc.save(function (err, data) {
        if (err) {
            return sendJsonResponse(res, 400, err);
        } else {
            return sendJsonResponse(res, 200, data);
        }
    });
});

#1


0  

define flag variable as

将flag变量定义为

flag=0 ; 

Then

else {
        flag++;
        sendJsonResponse(res, 401, {"message": "Wrong data entry."})
      }

Then

if (flag ==0 ){  
doc.save(function (err, data) {
            if (err) {
                sendJsonResponse(res, 400, err)
            } else {
                sendJsonResponse(res, 200, data);
            }
        });
 } 

#2


0  

In my opinion it should look like this:

在我看来,它应该是这样的:

function sendJsonResponse(res, status, content) {
  return res.status(status).json(content); // you can send response only one 
}

and:

companyTotal.findOne({companyName: "xyz"}, function (err, doc) {
    if (err) {
        return sendJsonResponse(res, 400, err);
    } else if (doc) {
            if (req.body.q1 === "poor") {
                doc.poor += 1;
            } else if (req.body.q1 === "okay") {
                doc.okay += 1;
            } else if (req.body.q1 === "well") {
                doc.well += 1;
            } else if (req.body.q1 === "very well") {
                doc.veryWell += 1;
            } else {
                return sendJsonResponse(res, 401, {"message": "Wrong data entry."});
            }
    }
    doc.save(function (err, data) {
        if (err) {
            return sendJsonResponse(res, 400, err);
        } else {
            return sendJsonResponse(res, 200, data);
        }
    });
});