Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.publish callback requires wrapping in Fiber

I get the following error whenever a client reloads/disconnects from the page. However the error disappears when I remove this part of the line of code involving the collection myCollection.insert(data).

What is happening here? No non-Meteor libraries seem to be involved..

server/publications.js

Meteor.publish('mySubscription', function() {

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

    return mySubscription.find();
});

Error

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

Using Meteor 0.7.0.1

like image 556
Nyxynyx Avatar asked Mar 20 '23 22:03

Nyxynyx


1 Answers

The callback inside .on will no longer be in a fiber. Try wrapping it with Meteor.bindEnvironment().

Like this:

this._session.socket.on('close', Meteor.bindEnvironment( function() {
    myCollection.insert(data)    // removing this line avoids the error
}, function( error) {console.log( error);});
like image 150
user728291 Avatar answered Apr 01 '23 15:04

user728291