Meteor 2.8 Async and Blaze

I have modified a first part of our application to use the …Async methods. Obviously they also appear at a certain point in a helper - e.g. just a simple Collection.findOneAsync, forcing the helper to become an async function. At that moment, the Template.instance().state.get(…) starts to fail.
Will that be addressed in a proper way in Blaze ? What’s an appropriate work around ? I’m now using a module level var to hold my reactive dictionary.

Maybe the answer on this one can help in properly awaiting for your async helper function:

1 Like

I digged a little deeper in the issue :

in a helper with this structure :

Template.myTemplate.helpers({
    async myHelper() {
      let foo = await Collection.findOneAsync({_id : '12345'});
      console.log(Template.instance().state.get('myvar') ;
    }
})

the error
Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘state’) at Object.myHelper
is thrown

Without the findOneAsync, everything is fine.

This means that references to Template.instance() are no longer valid after an ‘await SomeAsyncFunction’ in the helper.
Caching the Template.instance() in a local variable with
instance = Template.instance()
and referring to instance after the findOneAsync is a workaround but not a pretty one …

The type of behaviour like treated in this topic https://forums.meteor.com/t/how-to-wait-template-oncreated-before-render/51114 will be common practice. From the moment you need to retrieve some data in a onCreated method of a Template, you have to implement work arounds in the onRender, while the strict predictable flow of onCreated and onRendered is a nice pattern.

1 Like

Further investigation learned me that a call to an FunctionAsync yields Template.instance() to be void after that call.

e.g.

async MyHelper() {
    let rec = await Collection.findOneAsync({_id:"12345"};
    console.log(Template.instance().state.get('myVar'));
}

produces an error for Template.instance() to be undefined

@polygonwood the options property is actually not really AutoForm but from SimpleSchema and we already have a discussion about it going on, how we can maintain Meteor support for it:

https://forums.meteor.com/t/simple-schema-3

@storyteller we should put this on the agenda for the community Meeting tomorrow. The SimpleSchema issue might become bigger than expected.

Edit: @polygonwood the other issues (Async Helpers and Async Template hooks) should of course be discussed as well. There is also already a GitHub issue targeted for the next Major Release (3.0):

You can join the discussion there, too and we’d love to see you support the development with, for example, a review here and there, once a PR is provided.

3 Likes

@jkeuster I’m happy to help where possible. I will read the other thread as well. You’re right the options ia simple-schema, and you can work around (but again introducing extra refactoring work) by preparing the options outside the schema itself, but still the helper returning the schema to AutoForm becomes async and returns a promise which AutoForm can’t deal with (now). I looked a little deeper into AutoForm but as usual with asyn, it will probably spread out fast once you start to making AutoForm parts async …