添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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.

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.

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

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):

github.com/meteor/blaze we will get for the same Template a `toString` output of a pending Promise: `[object Promise]` However, we should be able to handle Promises in 2022 in any context. This would imply a huge PR but I think this is a crucial candidate for Blaze 3 and the future of Blaze

@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 …