Meteor Reactive Session:不工作(为什么?)

Meteor Reactive Session: Not Working (Why?)

我在 Meteor.js 中遇到反应式会话问题。

演示:Meteor Pad

Template.rows.helpers({
  'rows': function () {
     return Session.get('rows'); // data set in Session
  }
});

Template.count.events({
  'click .mdl-radio__button': function (e) {
    // target represents a number of selected rows (1, 2, 5, or 10)
    var value = $(e.currentTarget).val();
    Session.set('limit', value);
  },
  'click #reset': function () {
    Session.set('limit', 0);
    Session.set('rows', null);
  },
  'click #run': function () {
    // should only get rows when run() is pressed
    Session.set('rows', currentItems);
  }
});

用户应该可以select接收新的collections个数,受限额控制。但是,我不断收到以下错误:

Error: Match error: Failed Match.OneOf or Match.Optional validation

知道为什么吗?有人可以向我展示一个有效的 MeteorPad 演示吗?

我在使用你的 meteorpad 时遇到了问题。但你的问题不是 Session。问题是您对 Tracker.autorun 的使用。你应该阅读 docs on that.

您假设 Tracker.autorun(getItems) returns 什么 getItems returns。情况并非如此。您需要在 autorun 中设置 currentItems(在您的情况下为 getItems)。

getItems = function () {
  if (Session.get('limit') > 0) {
    currentItems = Items
      .find({}, {limit: Session.get('limit')})
      .map(function (item, index) {
        item.index = index + 1;
        return item;
      });
  } else {
    currentItems = null;
  }
};

终于想通了。显然 Session 创建了一个字符串,因此 Session.set('limit', 1) 将限制设置为 "1"。当然,字符串可以在Mongo收集请求中处理。

解决方案是使用 {limit: parseInt(Session.get('limit')}