Monday, 17 April 2017

If-else statements in query with node.js

var FilterAndSortByQuery = function(req,res,QuestionModel) {
    var query = req.query;
    QuestionModel
        .find(query, function (err, question) {
            if (err) {
                res.status(500).send(err)
            } else {
                if (query.OnlyNumber) {
                    res.json(question.length);
                } else {
                    res.json(question);
                }
            }
        })
};

Here I wrote a function which intake the query and return the filtered result from my QuestionModel. query.OnlyNumber is set to be true if only the number of result has to be returned and vice versa. It turns out that if query.OnlyNumber is false, the result works fine while if it is true, it return 0. I have tried several other ways in writing it and it seems that only the functions in "else" can work fine. Is there any reasons for that?



via Wesleywky

No comments:

Post a Comment