javascript 模板助手中的异常:TypeError:无法读取未定义的属性“配置文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25758476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in template helper: TypeError: Cannot read property 'profile' of undefined
提问by Jason Chung
Now I had a similar problem before where I was getting this error:
现在我在收到此错误之前遇到了类似的问题:
Exception in template helper: TypeError: Cannot read property 'profile' of undefined
模板助手中的异常:TypeError:无法读取未定义的属性“配置文件”
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}}?
同样的事情再次发生,但在第二个顺序上,其中包含另一个用户配置文件信息(第一个配置文件已定义)。我如何让它在 {{#each orders}} 中重新渲染?
It also appears that info.firstName, lastName, and building gets called 3 times for some reason when there are only 2 orders...
当只有 2 个订单时,似乎由于某种原因 info.firstName、lastName 和 building 被调用了 3 次......
In HTML:
在 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>
In javascript:
在 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
}
};
Appreciate the help!
感谢帮助!
回答by Kuba Wyrobek
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:
此错误是常见问题。您正在尝试访问未定义的用户对象。函数info
不检查user
对象是否正确。使用称为保护的技术:
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
}
};
Above code won't throw any error even if user is undefined
.
即使用户是undefined
.
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.
我假设你已经删除了autopublish
包。可能您还没有发布/订阅/订阅Meteor.users
集合,因此在 minimongo 中找不到数据。
Remember to publish Meteor.users
collection and subscribe to it:
记得发布Meteor.users
集合并订阅它:
Meteor.publish("users", function(){
return Meteor.users.find({},{fields:{profile:1}})
})
Meteor.subscribe("users");
Publish certain information for Meteor.users and more information for Meteor.user