[英]Specifying Mongo Query Parameters From Client Controller (MEAN.JS)
I am building an application using MongoDB, Angular, Express, and Node (MEAN stack). 我正在使用MongoDB,Angular,Express和Node(MEAN堆栈)构建应用程序。
I used the MEAN.JS generator to scaffold my application. 我使用MEAN.JS生成器来构建我的应用程序。
I will use the articles module as a reference. 我将使用文章模块作为参考。
Suppose I have 7000 records in my articles collection, and each record has a date associated with it. 假设我的文章集合中有7000条记录,并且每条记录都有一个与之关联的日期。 It is inefficient to load all 7000 records into memory every time I load the page to view the records in a table and I am seeing terrible performance losses because of it.
每次加载页面以查看表中的记录时,将所有7000条记录加载到内存中是低效的,因此我看到了可怕的性能损失。 For this reason, I would only like to load records with a date in the range of (1 Month Ago) to (1 Year From Now) and display them in the table.
出于这个原因,我只想加载日期范围为(1个月前)到(从现在起1年)的记录,并将其显示在表格中。 I can currently do this with the following:
我目前可以通过以下方式执行此操作:
In my articles.client.controller.js: 在我的articles.client.controller.js中:
$scope.find = function() {
$articles = Articles.query();
};
...and in my articles.server.controller.js: ...在我的articles.server.controller.js中:
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 of doing things. 问题是,这不是一种动态的做事方式。 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 (eg 'aYearFromNow' and 'aMonthAgo') in my client view that will change the query parameters in my server controller? 如何在我的客户端视图中绑定变量(例如'aYearFromNow'和'aMonthAgo'),这将改变我的服务器控制器中的查询参数?
Another way is to just pass the search parameters in the query method, like this: 另一种方法是在查询方法中传递搜索参数,如下所示:
$scope.searchart = function() {
Articles.query({start:$scope.startDate, end:$scope.endDate}, function(articles) {
$scope.articles = articles;
});
};
and then at the server side controller, read your query string parameters like this: 然后在服务器端控制器,读取您的查询字符串参数,如下所示:
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 way doesn't require more routes or services. 这种方式不需要更多路由或服务。
It's probably not the cleanest way, but you can create a new service (or edit the current one to work with 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
});
};
On the back-end, you'll need to prepare a route : 在后端,您需要准备一条路线:
app.route('/articles/:param1/:param2')
.get(articles.listWithParams)
Add a function to your back-end 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. 应该工作,但没有测试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.