【发布时间】:2016-10-26 12:42:42
【问题描述】:
为了呈现文档列表,我在从订阅返回数组的函数上使用.map():
{this.getApplications().map( (application) => {
return application.name;
})}
但是当我想像这样渲染单个文档时:
export default class ApplicationForm extends TrackerReact(React.Component) {
constructor() {
super();
this.state = {
subscription: {
applications: Meteor.subscribe('applications')
}
}
}
componentWillUnmount() {
this.state.subscription.applications.stop();
}
getSingleApplication() {
const applicationDoc = Applications.find().fetch();
if (applicationDoc) {
return applicationDoc[0];
}
}
render () {
const name = this.getSingleApplication().name;
return (
<div>
{name}
</div>
);
}
}
我收到以下错误:
无法读取未定义的属性“名称”
我想我错过了一些基本的 javascript。
或者它可能与页面加载时未准备好订阅有关?
【问题讨论】:
-
我们能看到更多代码吗?
-
this.getSingleApplication()返回未定义。我们无能为力。 -
Cannot read property 'name' of undefined- 表示你需要学习 javascript...getSingleApplication 返回 undefined... -
@KellyJAndrews
this.getSingleApplication()在订阅准备好之前返回 undefined,当订阅准备好时返回文档。 -
不知道 - 但这似乎很有用 - discovermeteor.com/blog/data-loading-react
标签: javascript reactjs meteor