Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment

Hello,

I am trying to insert into my mongo collection when I recieve messages from Legacy Server. I got errors like:

Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

So I tried bounding with below code but, it still gives me the same error.

const bound = Meteor.bindEnvironment((callback) => {callback();});
bound(() => {
if (err) {
console.log(err); // an error occurred
} else {
let game = {
date: new Date(),
// status: them.settings.autoaccept ? “playing” : “pending”,
status: “pending”,
requestBy:self.user._id,
legacy_id: p2[0],
clocks: {
white: { time: p2[6], inc: p2[5] },
black: { time: p2[8], inc: p2[7] }
},
white: {
name: p2[1],
rating: p2[12]
},
black: {
name: p2[2],
rating: p2[13]
},
moves: ,
actions:
}
console.log(game);
GameCollection.insert(game);
}
});

Please guide.

Unfortunately, you haven’t shared the section code with the callback. You may not even need the callback, so it would be helpful if you could provide the missing bit of code.

Also, please wrap your code in lines with triple-backticks, like this:

```
your
code
here
```

Thanks :slightly_smiling_face:

Also, that is not the correct way to use Meteor.bindEnvironment.

// the correct way is:
const callback = function (pararamters... ) {
  ...do something...
}
const boundCallback = Meteor.bindEnvironment(callback);

someAsyncFunction(foo, bar, boundCallback);

// or simply:
someAsyncFunction(foo, bar, Meteor.bindEnvironment((parameters...) => {
  ...do something...
});

For example, here’s some my my own code:

twilioClient.messages.list().then(Meteor.bindEnvironment((messages) => {
    console.log('sms', 'got data for', messages.length, 'messages...');
    // process message logs...
}));
1 Like