Thursday, 8 June 2017

How to Capture data from a query string and add it to a route to filter/sort - asc or desc nodejs mongodb mongoose ejs

This should be rather a simple one.... I creating a small app using nodejs with mongodb and ejs.

I can sort and filter fine when i define the options in the route - however, i need guidance - how do i capture data from the front end html query string and add it to the router options automatically...

here i my form -

<form action="/places" method="GET" class="">
                                <article class="form-inline">
                                  <div class="form-group">
                                    <label class="mr-sm-2" for="inlineFormCustomSelect">Per Page:</label>
                                    <select name="limit" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
                                      <option value="5">5</option>
                                      <option value="10">10</option>
                                      <option value="20">20</option>
                                      <option value="50">50</option>
                                      <option value="100">100</option>
                                    </select>
                                  </div>
                                  <label>Search:
                                    <input class="form-control post_search_text m-b" type="text" name="" placeholder="Enter a keyword"/>
                                  </label>
                                  <div class="form-group">
                                    <label class="mr-sm-2" for="inlineFormCustomSelect"> Order By:</label>
                                    <select name="sort" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
                                      <option value="title">Name</option>
                                      <option value="country">Country</option>
                                      <option value="category">Category</option>
                                      <option value="created">Date</option>
                                    </select>
                                    <select name="by" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
                                      <option value="asc">ASC</option>
                                      <option value="desc">DESC</option>
                                    </select>
                                  </div>
                                  <input class="btn btn-primary post_search_submit m-b" type="submit" value="Filter"/>
                                </article>
                            </form>

and here is my route

router.get("/places", function(req, res, next){
 var query = {}
 var options = {
     sort : { created: 1 },
     page: req.query.page,
     limit: req.query.limit
 }
console.log(req.query)
var q = q2m(req.query)

console.log(q)

Place.paginate(query, options, function(err, places) {
    // console.log(places)
    var pages = places.pages
    var total = places.total
    var place = places.docs
    // console.log(place)
    if (err) return next(err);


    res.format({
      html: function() {
        res.render('places', {
          places: place,
          pageCount: pages,
          itemCount: total,
          pages : res.locals.paginate.getArrayPages(3, places.pages, req.query.page)
        //   pages: paginate.getArrayPages(req)(3, places, req.query.page)
        });
      },
      json: function() {
        // inspired by Stripe's API response for list objects
        res.json({
          object: 'list',
          has_more: paginate.hasNextPages(req)(pages),
        //   has_more : res.locals.paginate.hasNextPages(camps.pages),
          data: places
        });
      }
    });
});

});

and here is the result of the two console.logs - the first is req.query and the second trying with query-to-mongo. the limits work fine.

{ limit: 5, sort: 'created', by: 'asc', page: 1 }

{ criteria: { by: 'asc', page: 1 },
  options: { sort: { created: 1 }, limit: 5 },
  links: [Function] }

i know it could be dumb - but any pointers or links will be appreciated...coz i've googled and read through not sure what little thing i am not getting right.

thanks...



via Joshua

No comments:

Post a Comment