Meteor.publish回调需要包装在Fiber中

Nyx*_*nyx 4 javascript node.js meteor

每当客户端重新加载/断开页面时,我都会收到以下错误.但是,当我删除涉及集合的代码行的这一部分时,错误消失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();
});
Run Code Online (Sandbox Code Playgroud)

错误

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
Run Code Online (Sandbox Code Playgroud)

使用Meteor 0.7.0.1

use*_*291 7

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

像这样:

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