Specifying Mongo Request Parameters from a Client Controller (MEAN.JS)

I am building an application using MongoDB, Angular, Express and Node (MEAN stack).

I used the MEAN.JS generator to search for my application.

I will use the article module as a link.

Suppose I have 7,000 entries in my collection of articles, and each entry has a date associated with it. It is inefficient to load all 7000 records into memory every time I load a page to view records in a table, and because of this I see terrible performance losses. For this reason, I would just like to download records with a date in the range from 1 month to (1 year from the moment) and display them in a table. I can currently do this with the following:

In my .client.controller.js articles:

$scope.find = function() { $articles = Articles.query(); }; 

... and in my .server.controller.js articles:

 var now = new Date(); var aYearFromNow = new Date(now.getTime() + 86400000*365); //add a year var aMonthAgo = new Date(now.getTime() - 86400000*30); //subtract roughly a month exports.list = function(req, res) { Article.find().where('date').lt(aYearFromNow).gt(aMonthAgo).sort('-created').populate('user', 'displayName').exec(function(err, articles) { if (err) { return res.send(400, { message: getErrorMessage(err) }); } else { res.jsonp(articles); } }); }; 

The problem is that this is not a dynamic way to do something. In other words, I want the user to be able to specify how far back and how far forward they want to see.

How can I bind to variables (for example, "aYearFromNow" and "aMonthAgo") in my client view that will change the request parameters in my server controller?

+7
javascript angularjs mongodb express meanjs
source share
2 answers

This is probably not the cleanest way, but you can create a new service (or edit the current one for several parameters):

 .factory('ArticlesService2', ['$resource', function($resource) { return $resource('articles/:param1/:param2', { param1: '', param2: '' }, { update: { method: 'PUT' } }); } ]); 

Then call it in your controller:

 $scope.findWithParams = function() { $scope.article = ArticlesService2.query({ param1: $scope.aYearFromNow, param2: $scope.aMonthAgo }); }; 

In the background, you need to prepare a route:

 app.route('/articles/:param1/:param2') .get(articles.listWithParams) 

Add a function to your internal controller:

 exports.listWithParams = function(req, res) { Article.find() .where('date') .lt(req.params.param1) .gt(req.params.param2) .sort('-created').populate('user', 'displayName') .exec(function(err, articles) { if (err) { return res.send(400, { message: getErrorMessage(err) }); } else { res.jsonp(articles); } }); }; 

Should work, haven't tested it though.

+3
source share

Another way is to simply pass the search parameters in the query method, for example:

  $scope.searchart = function() { Articles.query({start:$scope.startDate, end:$scope.endDate}, function(articles) { $scope.articles = articles; }); }; 

and then on the server controller read the query string parameters, for example:

 exports.searcharticle = function(req, res) { Article.find().where('date').gt(req.query['start']).lt(req.query['end']).exec(function(err, articles) { if (err) { res.render('error', { status: 500 }); } else { res.jsonp(articles); } }); }; 

This path does not require more routes or services.

+7
source share

All Articles