【问题标题】:Meteor - cursor with limit set isn't reactiveMeteor - 设置了限制的游标没有反应
【发布时间】:2014-12-04 00:51:48
【问题描述】:

我不知道这是否是错误,但是当我指定 limit 时,如果客户端集合中已经存在与查询匹配的某些数据,则客户端上的光标不会反应。

例如,如果有限制:4 并且已经有 1 条记录匹配,则 它返回一条记录,当接下来的 3 条记录(查询查询)从服务器到达时,游标没有反应(我期待 它将被再次评估并返回所有这 4 条记录)。

我找到它是因为当我取消注释我正在获取所有记录的行时,我的应用程序可以工作(因为该光标将反映新数据可用)。你可以看到查询是一样的,只是limit

messages = Messages.find(selector, {sort: {created: -1}, limit: MessagesAPI.LIMIT}).fetch();
 //Messages.find(selector, {sort: {created: -1}}).fetch()); 
 // if i uncomment the previous line, it works

更多代码

getMeteorState: function () {
        console.log("zde");
        var time = this.getParams().time;
        var dir = this.getParams().dir;
        //TODO: maybe check time and dir validity or let it crash ?
        var ready = Session.get("messages-ready");
        var params = {sort: MessagesAPI.sort.NEW, dir: dir == "prev" ? MessagesAPI.dir.PREV : MessagesAPI.dir.NEXT};
        if (time) {
            var d = new Date();
            d.setTime(time);
            params.date = d;
        }
        Meteor.subscribe("messages", params, function () {
            console.log("ready");
            Session.set("messages-ready", true);
        });

        var messages = [];
        if (ready) {
            var selector = {};
            if (time && dir) {
                selector.created = {};
                var cond = (dir == "prev" ? "$lt" : "$gt");
                var date = new Date();
                date.setTime(time);
                selector.created[cond] = date;
            }
            messages = Messages.find(selector, {sort: {created: -1}, limit: MessagesAPI.LIMIT}).fetch();
            //console.log(selector);
             // when i uncomment this, it will work
            //console.log(Messages.find(selector, {sort: {created: -1}}).fetch());
        }
        return {
            messages: messages
        };
    },

【问题讨论】:

  • 如果你关闭 .fetch() 会发生什么?
  • @KeithNicholas:在消息中将只是一些 LocalCollection 实例
  • 你是如何使用它的?
  • @KeithNicholas:我正在为我的 UI 使用 React,所以它可能很难解码,但我会更新问题
  • 当你调用 .fetch 时,你得到的数组不是响应式的

标签: meteor


【解决方案1】:

它是反应性的。

如果我创建一个默认应用并像这样修改它

Messages = new Mongo.Collection("messages");

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault("counter", 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get("counter");
    },
    messages: function() {
      var messages = Messages.find({},{sort: {text: -1},  limit: 4}).fetch();
      return messages;
    }
  });

  Template.hello.events({
    'click button': function () {      
      Session.set("counter", Session.get("counter") + 1);
      Messages.insert({text: Session.get("counter")});
    }
  });
}

和html

<head>
  <title>reactive</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}

</body>

<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>

  <div>
  {{#each messages}}
    {{text}}
  {{/each}}
  </div>
</template>

没问题。通过 Mongo 控制台手动插入响应式更新

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 2016-06-28
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多