模板助手中的异常:TypeError:无法读取未定义的属性“profile” [英] Exception in template helper: TypeError: Cannot read property 'profile' of undefined

查看:163
本文介绍了模板助手中的异常:TypeError:无法读取未定义的属性“profile”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在遇到此错误之前遇到了类似的问题:

Now I had a similar problem before where I was getting this error:


模板助手中的异常:TypeError:无法读取属性
'profile'of undefined

Exception in template helper: TypeError: Cannot read property 'profile' of undefined

同样的事情再次发生,但在第二个订单上,其中包含另一个用户个人资料信息(第一个配置文件已定义)。
我如何让它在{{#each orders}}中重新呈现?

The same thing is happening again but on the second order, which contains another users profile information (the first profile is defined). How would I get it to re-render in {{#each orders}}?

看来info.firstName,lastName和building得到了由于某种原因,当只有2个订单时被调用3次...

It also appears that info.firstName, lastName, and building gets called 3 times for some reason when there are only 2 orders...

在HTML中:

<template name="orderItem">
  <section>
    <form role="form" id="ordersList">
      <div>
        {{#each orders}}
          <input type="text" name="name" value="{{info.firstName}} {{info.lastName}}">
        {{/each}}
      </div>
      <div>
        {{#each orders}}
          <input type="text" name="building" value={{info.building}}>
        {{/each}}
      </div>
      <div>
        {{#each orders}}
          <input type="text" name="featuredDish" value={{featuredDish}}>
        {{/each}}
      </div>
    </form>
  </section>
</template>

在javascript中:

In javascript:

Template.orderItem.orders = function() {
  var todaysDate = new Date();
  return Orders.find({dateOrdered: {"$gte": todaysDate}});
};

Template.orderItem.info = function() {
  var userId = this.userId;
  var user = Meteor.users.findOne(userId)
  var firstName = user.profile.firstName;
  var lastName = user.profile.lastName;
  var building = user.profile.building;

  return {
    firstName: firstName,
    lastName: lastName,
    building: building
  }
};

感谢帮助!

推荐答案

此错误是常见问题。
您正在尝试访问未定义的用户对象。
函数 info 不检查 user 是否是正确的对象。使用名为守卫的技术:

This error is common issue. You are trying to access user object which is undefined. Function info doesn't check if user is correct object. Use technique called guarding :

Template.orderItem.info = function() {
  var userId = this.userId;
  var user = Meteor.users.findOne(userId)

  var firstName = user && user.profile && user.profile.firstName;
  var lastName = user && user.profile  && user.profile.lastName;
  var building = user && user.profile  && user.profile.building;

  return {
    firstName: firstName,
    lastName: lastName,
    building: building
  }
};

即使用户 undefined,上述代码也不会抛出任何错误

我假设你已经删除了 autopublish 包。
可能你没有发布/订阅 Meteor.users 集合,所以没有数据可以在minimongo中找到。

I assume that you have removed autopublish package. Probably you haven't published/subscribed from/to Meteor.users collection, so there is no data to find in minimongo.

请记住发布 Meteor.users 集合并订阅它:

Remember to publish Meteor.users collection and subscribe to it:

Meteor.publish("users", function(){
  return Meteor.users.find({},{fields:{profile:1}})
})

Meteor.subscribe("users");

为Meteor.users发布某些信息,并为Meteor.user发布更多信息

这篇关于模板助手中的异常:TypeError:无法读取未定义的属性“profile”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆