我想通过距离函数向显微镜应用添加额外的排序。
问题是列表仍未排序,仍然会将完整列表抛给我。服务器明智,没有错误被抛出,所以我猜发布工作正常。启动服务器时唯一的服务器错误如下:
子帖子的异常比例ID KcEizuxZWk3ifm6A9错误: 轮询查询时出现异常 { “集合名”: “上岗”, “选择”:{ “位置”:{ “$近”:{ “$几何”:{ “类型”: “点”, “坐标”:NULL,NULL]}, “$ maxDistance”:500}}}, “选项”:{ “改造”:空}}: $ near需要一个点,给定{type:“Point”,坐标:[null, null]}
Theres端没有显示错误。位置控制台日志显示正确的位置。
哪些部分不好或者建议如何检查错误/排除故障?特别是对于客户端/前端控制台检查等。
显微镜将2种排序功能集中在一个路由器中,如下所示:
PostsListController = RouteController.extend({
template: 'postsList',
increment: 5,
postsLimit: function() {
return parseInt(this.params.postsLimit) || this.increment;
},
findOptions: function() {
return {
sort: this.sort,
limit: this.postsLimit() };
},
subscriptions: function() {
this.postsSub = Meteor.subscribe('posts', this.findOptions());
},
posts: function() {
return Posts.find({}, this.findOptions());
},
postsByDistance: function() { // suggested by Brian, still doesnt work
return Posts.find({}, this.findOptions());
},
data: function() {
var self = this;
return {
posts: self.posts(),
ready: self.postsSub.ready,
nextPath: function() {
if (self.posts().count() === self.postsLimit())
return self.nextPath();
}
};
}
});
NewPostsController = PostsListController.extend({
sort: {submitted: -1},
nextPath: function() {
return Router.routes.newPosts.path({ postsLimit: this.postsLimit() + this.increment })
}
});
BestPostsController = PostsListController.extend({
sort: {votes: -1},
nextPath: function() {
return Router.routes.bestPosts.path({postsLimit: this.postsLimit() + this.increment})
}
});
// What I have inserted
NearPostsController = PostsListController.extend({
sort: {postsByDistance: -1}, // removing this throws "no sort key" error
nextPath: function() {
return Router.routes.nearPosts.path({postsLimit: this.postsLimit() + this.increment})
}
});
Router.route('/', { name: 'home', controller: NewPostsController });
Router.route('/new/:postsLimit?', { name: 'newPosts'} );
Router.route('/best/:postsLimit?', { name: 'bestPosts'} );
Router.route('/near/:postsLimit?', { name: 'nearPosts'} ); // What I inserted
服务器js
Posts._ensureIndex({'location' : '2dsphere'});
Meteor.publish('postsByDistance', function(location) {
return Posts.find({
location: {
$near: { $geometry: {type:"Point", coordinates: [location.lng, location.lat]
}, $maxDistance: 500 ......etc etc
客户订阅(由新的,最好的帖子共享,但他们的代码都在路由器上)
Meteor.subscribe("postsByDistance", location);
posts: function() { return Posts.find(); } //under helper... I tried to add sort here but doesnt work