Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

mongoose and express - how to combine two functions into one output

mongoose and express - how to combine two functions into one output

Problem

i'm trying to make search form for my site, with two separate inputs, one for title keywords and the other one for body of the post. I don't know how to pass these 2 variables (asd for title and asdd for body) to Function, thats my app.js file :

app.get('/search', function(req, res) {
    postdb.findByTitle(asd, function(error, article) {
        res.render('search.jade',
        { locals: {
            title: article.title,
            articles:article
        }
        });
    });
});

and here is function for finding (check the bold parts):

PostDB.prototype.findByTitle = function(**asd asdd**, callback) {
    this.getCollection(function(error, article_collection) {
      if( error ) callback(error)
      else {
        article_collection.find({**title: asd, body:asdd**}).toArray(function(error, results) {
          if( error ) callback(error)
          else callback(null, results)
        });
      }
    });
};
Problem courtesy of: Spy

Solution

Pass a couple of query string params with the url to /search.

For example:

 /search?title=asd&body=asdd;

And then use the req object to grab them and pass to your function:

app.get('/search', function(req, res) {
    var title = req.query.title
       ,body = req.query.body;

    postdb.findByTitle(title, body, function(error, article) {
        res.render('search.jade',
        { locals: {
            title: article.title,
            articles:article
        }
        });
    });
});
Solution courtesy of: c0deNinja

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

mongoose and express - how to combine two functions into one output

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×