我对 Meteor 还很陌生,所以我可能会在这里错过一个关键的见解。
无论如何,我想向用户表明有多少其他用户同时在该网站上。我已经有一个 AuditItems
集合,它存储我可以用于的 who
、when
和 what
的 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)
最佳答案
所有Deps
方法只能在Client上使用。您收到的错误与 Deps 的构建方式有关(因为客户端不使用纤程)。
如果您想在服务器上进行 react ,您需要使用 observe
或observeChanges
。使用添加和删除的句柄,您可以查询当时的日期(当文档更改时)。
您还可以使用Meteor.setInterval
定期删除旧用户。
您可以做的事情之一是使用像 meteor-prescence 这样的包这一切都为你完成。它的作用是保存一个实时集合,其中包含每个在线用户的信息,然后当他们离线/超时后,它会使用定期的 Meteor.setInterval
方法将它们从集合中删除。
关于javascript - Meteor,服务器上的错误 : Can't wait without a fiber using Meteor. setTimeout(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18806305/