【问题标题】:Meteor, Error: Can't wait without a fiber using Meteor.setTimeout() on the serverMeteor,错误:在服务器上使用 Meteor.setTimeout() 无法等待没有光纤
【发布时间】:2013-09-14 20:53:31
【问题描述】:

我对 Meteor 还很陌生,所以我可能在这里错过了一个关键的见解。

无论如何,我想向用户表明同时有多少其他用户在网站上。我已经有一个AuditItems 集合,它存储了whowhenwhat 的kv 对,我可以将它们用于这种类型的计算。我的Collections 查询是使用new Date() 参数运行的,所以我不能只观察结果,我必须定期重新运行查询。

然后,我通过在 Deps.Dependency 上调用 changed使其具有反应性。

代码如下:

  // publishing counts of the users that are logged on
  // at the moment
  Meteor.publish("user-activity", function () {
    var self = this;

    self.added("user-activity", "all-recent-activity", {'operations': getRecentActivityCountsCache});
    self.ready();

    Deps.autorun(function() {
      self.changed("user-activity", "all-recent-activity", {'operations': getRecentActivityCounts()});
    });
  });

  var getRecentActiveCountsDependency = new Deps.Dependency;
  var getRecentActivityCountsCache = 0;
  var getRecentActivityCounts = function() {

    // register dependency with the caller
    getRecentActiveCountsDependency.depend();

    var now = new Date();
    var aWhileAgo = new Date(now.valueOf() - (5 * 60 * 1000)); // 5 minutes

    auditItems = AuditItems.find({'when': { '$gt': aWhileAgo }}).fetch();

    console.log('raw data: ' + JSON.stringify(auditItems));

    getRecentActivityCountsCache = _.chain(auditItems)
      .groupBy('who')
      .keys()
      .size()
      .value();

    console.log('new count: ' + getRecentActivityCountsCache);

    return getRecentActivityCountsCache;
  };

  Meteor.setTimeout(function() {
    getRecentActiveCountsDependency.changed();
  }, 60 * 1000); // 60 seconds

第一次触发计时器时,我在控制台上收到此错误:

    Exception from Deps recompute: Error: Can't wait without a fiber
        at Function.wait (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/fibers/future.js:83:9)
        at Object.Future.wait (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/fibers/future.js:325:10)
        at _.extend._nextObject (packages/mongo-livedata/mongo_driver.js:540)
        at _.extend.forEach (packages/mongo-livedata/mongo_driver.js:570)
        at _.extend.map (packages/mongo-livedata/mongo_driver.js:582)
        at _.extend.fetch (packages/mongo-livedata/mongo_driver.js:606)
        at _.each.Cursor.(anonymous function) [as fetch] (packages/mongo-livedata/mongo_driver.js:444)
        at getRecentActivityCounts (app/server/server.js:26:70)
        at null._func (app/server/server.js:12:79)
        at _.extend._compute (packages/deps/deps.js:129)

【问题讨论】:

    标签: javascript node.js meteor


    【解决方案1】:

    所有Deps 方法只能在Client 上使用。您得到的错误与构建 Deps 的方式有关(因为客户端不使用光纤)。

    如果您想在服务器上进行反应,您需要使用observeobserveChanges。使用添加和删除的句柄&您可以查询当时的日期(当文档更改时)。

    您还可以使用Meteor.setInterval 定期从那里删除旧用户。

    您可以做的一件事是使用像meteor-prescence 这样的包,它可以为您完成所有工作。它的作用是保存一个实时集合,其中包含每个在线的人的信息,然后当他们离线/超时后,它会使用周期性的Meteor.setInterval 方法将它们从集合中删除。

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 2017-11-04
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 2016-11-27
      • 2021-10-06
      • 2015-09-22
      相关资源
      最近更新 更多