Meteor.publishcallback需要在光纤中包装

无论何时客户端重新加载/断开页面,我都会收到以下错误消息。 但是,当我删除涉及集合myCollection.insert(data)这部分代码时,该错误消失。

这里发生了什么? 似乎没有非meteor库参与..

服务器/ publications.js

 Meteor.publish('mySubscription', function() { this._session.socket.on('close', function() { myCollection.insert(data) // removing this line avoids the error }); return mySubscription.find(); }); 

错误

 packages/mongo-livedata.js:3022 throw e; ^ Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment. at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:65) at null.<anonymous> (packages/meteor/helpers.js:108) at MongoConnection.(anonymous function) [as remove] (packages/mongo-livedata/mongo_driver.js:561) at Meteor.Collection.(anonymous function) [as remove] (packages/mongo-livedata/collection.js:447) at SockJSConnection.<anonymous> (app/server/publications.js:33:26) at SockJSConnection.EventEmitter.emit (events.js:117:20) at Session.didTimeout (/Users/username/.meteor/packages/livedata/ab19e493b9/npm/node_modules/sockjs/lib/transport.js:210:23) at WebSocketReceiver.GenericReceiver.didAbort (/Users/username/.meteor/packages/livedata/ab19e493b9/npm/node_modules/sockjs/lib/transport.js:296:35) at thingy_end_cb (/Users/username/.meteor/packages/livedata/ab19e493b9/npm/node_modules/sockjs/lib/transport.js:280:22) at EventEmitter.emit (events.js:95:17) => Exited with code: 8 => Meteor server restarted 

使用meteor0.7.0.1

.on内部的callback将不再是光纤。 尝试用Meteor.bindEnvironment()包装它。

喜欢这个:

 this._session.socket.on('close', Meteor.bindEnvironment( function() { myCollection.insert(data) // removing this line avoids the error }, function( error) {console.log( error);});