0

I am using the linnovate MEAN boilerplate to build an application: https://github.com/linnovate/mean

With MongoDB I understand how to query a collection on the database and get the results via the command line such as:

db.articles.find({ 'title' : 'hello world'})

In the MEAN app the area I noticed for this type of querying is in the app/controllers/articles.js file:

/**
* List of Articles
*/
exports.all = function(req, res) {
    Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
        if (err) {
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(articles);
        }
    });
};

If I wanted to add a way of returning another list with a specific query how would I go about that?Here is the code I'm working on:

exports.all = function(req, res) {
    Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
        if (err) {
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(articles);
        Article.find({ 'category' : 'hello world').sort('-created').populate('user', 'name username').exec(function(err, morearticles) {
            if (err) {
                res.render('error', {
                    status: 500
                });
            } else {
                res.jsonp(morearticles);
            }
        });
        }
    });
};

1 Answer 1

0

Due to its asynchronous nature, you have to place your second query within the else-statement of your first.

Please see this questions for further information:

rendering results of multiple DB/mongoose queries to a view in express.js

Two database queries in Mongoose with Express

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.