How to return Node.js callback How to return Node.js callback mongodb mongodb

How to return Node.js callback


You can't return from a callback - see this canonical about the fundamental problem. Since you're working with Mongoose you can return a promise for it though:

var getApps = function(searchParam){    var appsInCategory = Model.find({ categories: searchParam});    return appsInCategory.exec().then(function (apps) {        return apps; // can drop the `then` here    });}

Which would let you do:

getApps().then(function(result){    // handle result here});