从流星中的集合中检索数据

时间:2019-01-09 20:18:56

标签: javascript meteor

我正在尝试显示以表格形式提交的所有信息的总和。

Template.SingleDailylog.helpers({
  personInCharge: ()=>{
    const id = FlowRouter.getParam('id');
    const profile = Dailylog.findOne({_id:id});
    const name = profile.personInCharge;
    return name;
    }
});
<div class="form-group col-md-6">
    <input value="{{personInCharge}}" type="text" class="form-control" placeholder="Name">
    <label for="first">Person In Charge</label>
</div>

这确实插入了信息,但是我仍然遇到错误:

  

meteor.js?hash = 0504f43f667698535416b00eb44eb6f53161cb63:1048模板助手中的异常:TypeError:无法读取未定义的属性'personInCharge'       在Object.personInCharge(http://localhost:3000/app/app.js?hash=e537a3bd311bc41765fe473a7cd9cf9609139dc9:8544:26)       在http://localhost:3000/packages/blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3051:16       在http://localhost:3000/packages/blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:1715:16       在http://localhost:3000/packages/blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3103:66       在Function.Template._withTemplateInstanceFunc

我如何得到一个错误,但是显示的数据是正确的?这使我无法保存对数据的编辑。

1 个答案:

答案 0 :(得分:1)

The helper is trying to access a nested value (personInCharge) from an object that does not exist yet (profile)

If you want to prevent this exception from occurring, you have two options here:

Option 1 - Prevent access to undefined Objects inside the helper

You could for example maybe wrap each of your variables in an if statement like so:

Template.SingleDailylog.helpers({
  personInCharge: ()=>{
    const id, profile, name;

    id = FlowRouter.getParam('id');

    if (id) {
        profile = Dailylog.findOne({_id:id});
    }
    if (profile && profile.personInCharge) { // I always check nested things this way
        name = profile.personInCharge;
    }
    if (name) {
        return name;
    }

});

In this case, if id and profile and profile.personInCharge are undefined, the code in the if blocks won't execute, and therefore it won't be trying to access nested variables that don't exist yet when the template is created, which will keep the helper from throwing exceptions.

Option 2 - Prevent the helper from being called

You could also use a reactive variable to indicate, whether the subscription is ready and prevent the template from calling the helper, if not.

// const subscription = //... use this if you use a global suscription

Template.SingleDailylog.onCreated (function () {
  const instance = this;
  instance.state = new ReactiveDict();
  instance.autorun(() => {
    const subscription = //... use this for Template level subscription
    if (subscription.ready()) {
      instance.state.set('loadComplete', true);
    }
  });
})

Then add a helper for loadComplete:

Template.SingleDailylog.helpers({
  personInCharge() {
    const id = FlowRouter.getParam('id');
    const profile = Dailylog.findOne({_id:id});
    const name = profile.personInCharge;
    return name;
  },
  loadComplete () {
    return Template.instance().state.get('loadComplete');
  }
});

and use it to call the personInCharge helper only if loadComplete is true:

<div class="form-group col-md-6">
    {{#if loadComplete}}
    <input value="{{personInCharge}}" type="text" class="form-control" placeholder="Name">
    <label for="first">Person In Charge</label>
    {{else}}
    <div>Loading....</div>
    {{/if}}
</div>