【问题标题】:retrieve data from a collection in meteor从流星的集合中检索数据
【发布时间】:2019-06-04 16:10:32
【问题描述】:

我正在尝试显示在表单中提交的所有信息的总和。

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:16http://localhost:3000/packages/blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:1715:16http://localhost:3000/packages/blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3103:66 在 Function.Template._withTemplateInstanceFunc

我怎么会出错,但显示的数据是正确的?这使我无法保存对数据的编辑。

【问题讨论】:

  • 这很可能是因为在第一次渲染中订阅还没有准备好,因此助手抛出了一个错误。然后,一旦 sub 准备好,数据就可用,没有错误,并且您的输入值设置为任何名称。

标签: javascript meteor


【解决方案1】:

帮助程序正在尝试从尚不存在的对象 (profile) 访问嵌套值 (personInCharge)

如果你想阻止这个异常的发生,你有两个选择:

选项 1 - 阻止访问帮助程序中未定义的对象

例如,您可以将每个变量包装在 if 语句中,如下所示:

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;
    }

});

在这种情况下,如果 idprofileprofile.personInCharge 未定义,则 if 块中的代码将不会执行,因此它不会尝试访问不存在的嵌套变量然而,当模板被创建时,这将防止助手抛出异常。

选项 2 - 阻止调用助手

您还可以使用反应变量来指示订阅是否准备就绪,如果没有,则阻止模板调用帮助程序。

// const subscription = //... 如果你使用全局订阅,请使用这个

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);
    }
  });
})

然后为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');
  }
});

仅当loadComplete 为真时才使用它来调用personInCharge 助手:

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

【讨论】:

  • 我添加了另一个选项,因此您可以选择更适合您当前代码的选项。
  • @Jankapunkt 非常感谢您的回复。我尝试了选项1,错误消失了!!但我不确定如何操纵它来获得约会。使用代码:
  • Template.SingleDailylog.helpers({ date: function(){ const id = FlowRouter.getParam('id'); if (id){ profile = Dailylog.findOne({_id:id}) ; } if (profile && profile.date){ name = profile.date;} if (name){ return moment(name).format('MM/DD/YYYY');} },我收到错误:弃用警告: 提供的值不是公认的 ISO 格式。moment 构造回退到 js Date(),这在所有浏览器和版本中并不可靠
  • @Kassie 如果解决方案对您有帮助,请点赞并接受(注意,选项 1 由 @Ticdoc 提供)。关于您的其他问题,我建议您针对日期错误打开一个新问题(因为该错误与 Meteor 或模板助手无关)。
猜你喜欢
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多