noobprogrammer
noobprogrammer

Reputation: 345

How to use promises/callbacks using expressjs and mongodb?

I am really confused about how promises and async javascript work. I want to do a GET on /user and query mongo so that the result of the first query is processed in some way to form the query of the second query, and the result of that query be the query of the third query.

Basically I want the result of the final mongo result to be sent back to the client via res.send(result).

What's the proper way of doing this so that the client gets a 200 OK back with the result of the third nested mongo query?

app.get('/user', function (req, res, next) {

    var query = {"isRegistered": false }

    db.collection('users', function (err, collection) {
        collection.find(query).toArray(function (err, result) {
            if (err) {
                console.log(err)
            }
            else {
                if (result.length > 0) {
                     // do some random processing 
                     var randomUser = result[Math.floor(Math.random() * result.length)] 
                    // do ANOTHER db query
                    query = {"age": randomUser.age}
                    collection.find(query).toArray(function (err,result) {
                         if (err) {
                            console.log(err)
                         }
                         else {
                           // do some other logic ...
                           query = {something}
                           collection.find(query).toArray(function (err,result) {
                               if (err) {
                                 console.log(err);
                               }
                               else {
                                  // FINALLY RETURN THIS RESULT
                                  res.send(result);
                                  next()
                               }
                           })
                         }
                    })
                }
            }
        });
    });

Upvotes: 1

Views: 102

Answers (1)

adeneo
adeneo

Reputation: 318192

Assuming you're using the regular Mongo client for Node, it already returns promises for queries, meaning you can just return the next query and catch it in a then, or catch an error at the end with catch.

Untested, but something similar to this should work

app.get('/user', function (req, res, next) {

    var query = {"isRegistered" : false };
    var collection = db.collection('users');

    collection.find(query).toArray().then( result => {
        var randomUser = result[Math.floor(Math.random() * result.length)];
        var query2     = {"age" : randomUser.age};

        return collection.find(query2).toArray();
    }).then( result => {
        var query3 = {something : 'something'};

        return collection.find(query3).toArray();
    }).then( result => {
        res.status(200);
        res.send(result);
    }).catch( err => {
        console.log(err);
        res.status(500);
    });
});

Note that methods like toArray automatically returns a promise if no callback function is passed.

Upvotes: 2

Related Questions