【发布时间】:2018-05-24 18:51:11
【问题描述】:
我有一个 Meteor-react 应用程序,其中包含一个集合,包含大量数据。我用分页显示数据。
在服务器端,我只是发布当前页面的数据。
所以,我在服务器端发布一些数据:
Meteor.publish('animals', function(currPage,displayPerPage, options) {
const userId = this.userId;
if (userId) {
const currentUser = Meteor.users.findOne({ _id: userId });
let skip = (currPage - 1) * displayPerPage;
if (displayPerPage > 0) {
Counts.publish(this, 'count-animals', Animals.find(
{$and: [
// Counter Query
}
), {fastCount: true});
return Animals.find(
{$and: [
// Data query
]}, {sort: options.sortOption, skip: skip, limit: displayPerPage });
} else {
Counts.publish(this, 'count-animals', 0);
return [];
}
}
});
在客户端,我正在使用跟踪器:
export default AnimalsContainer = withTracker(({subscriptionName, subscriptionFun, options, counterName}) => {
let displayPerPage = Session.get("displayPerPage");
let currPage = Session.get("currPage");
let paginationSub = Meteor.subscribe(subscriptionName, currPage, displayPerPage, options );
let countAnimals = Counts.get(counterName);
let data = Animals.find({}).fetch({});
// console.log(data);
return {
// data: data,
data: data.length <= displayPerPage ? data : data.slice(0, displayPerPage),
countAnimals: countAnimals,
}
})(Animals);
问题是:
当我尝试在客户端修改排序选项时,服务器不是从第一个数据排序(跳过第一个数据)。有时从 20 日起有时从 10 日起。 类型检查是在两边完成的。
【问题讨论】:
-
Counts.publish与您的问题相关吗? -
它不应该是相关的,因为它只是计数。
-
请记住,当您使用排序 + 限制时,使用不同的排序可能会极大地改变已发布的文档。如果您只想对客户端收到的文档进行排序,您应该在客户端而不是服务器端进行排序。
-
感谢您的信息,但我需要在服务器端进行排序,并且还必须从服务器端限制它,因为性能更快。 (有更多的对象)我认为排序应该首先发生,然后限制它。不应该吗?
标签: mongodb reactjs meteor meteor-publications