Meteor [Error: cannot wait without fiber] after calling Email.send

I created a very simple server using Meteor to send an email after a timeout. When I use the timeout, the message is sent successfully, but an error occurs: [Error: Can't wait without a fiber] .

Here is my code:

 if (Meteor.isServer) { Meteor.startup(function () { // <DUMMY VALUES: PLEASE CHANGE> process.env.MAIL_URL = 'smtp://me%40example.com: PASSWORD@smtp.example.com :25'; var to = ' you@example.com ' var from = ' me@example.com ' // </DUMMY> // var subject = 'Message' var message = "Hello Meteor" var eta_ms = 10000 var timeout = setTimeout(sendMail, eta_ms); console.log(eta_ms) function sendMail() { console.log("Sending...") try { Email.send({ to: to, from: from, subject: subject, text: message }) } catch (error) { console.log("Email.send error:", error) } } }) } 

I understand that I could use Meteor.wrapAsync to create fiber. But wrapAsync expects there will be a callback for the call, and Email.send does not use the callback.

What should I do to get rid of the error?

+5
source share
1 answer

This is because while your Meteor.startup function works inside Fiber (like almost all other Meteor callbacks), you are not using setTimeout ! Due to the nature of setTimeout it will work in the upper region, outside the fiber in which you defined and / or called the function.

To solve, you can use something like Meteor.bindEnvironment :

 setTimeout(Meteor.bindEnvironment(sendMail), eta_ms); 

And then do this for each setTimeout call, which is a painfully hard fact.
It’s good that this is actually not so. Just use Meteor.setTimeout instead of your own:

 Meteor.setTimeout(sendMail, eta_ms); 

From the docs:

These functions work just like their native JavaScript equivalents. If you call the native function, you get an error message indicating that Meteor code should always work inside Fiber and we recommend using Meteor.bindEnvironment

Meteor timers simply bindEnvironment then delay the call as you wish.

+9
source

Source: https://habr.com/ru/post/1232147/


All Articles