首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >流星,错误:不能在服务器上没有使用Meteor.setTimeout()的光纤等待

流星,错误:不能在服务器上没有使用Meteor.setTimeout()的光纤等待
EN

Stack Overflow用户
提问于 2013-09-14 20:53:31
回答 1查看 1.3K关注 0票数 2

我对Meteor相当陌生,所以我在这里可能缺少一个关键的洞察力。

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

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

以下是代码:

代码语言:javascript
复制
  // 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

当计时器第一次触发时,我在控制台上得到了这个错误:

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

发布于 2013-09-15 06:27:30

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

如果您希望在服务器上具有反应性,则需要使用observeobserveChanges。使用添加和删除的句柄&您可以在此时(文档更改时)查询日期。

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

您可以做的一件事是使用像流星这样的包,它为您完成了所有的任务。它所做的是保存一个包含每个在线用户信息的实时集合,然后当他们离线/超时后使用定期的Meteor.setInterval方法将他们从集合中移除。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18806305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档