【发布时间】:2017-09-24 07:10:01
【问题描述】:
我正在尝试从初始状态对象映射对象属性。除了嵌套的milesotnes,我可以获得所有属性。
初始状态在组件类构造函数中定义如下:
class GoalView extends Component {
constructor(props) {
super(props);
this.state = {
dataGoal: {},
uid: firebaseAuth().currentUser.uid,
currentGoalId: this.props.match.params.goalId,
};
}
componentDidMount = () => {
const dataGoalRef = dbRef.child('goals/'+this.state.uid+'/'+this.state.currentGoalId);
dataGoalRef.on('value', snap => {
this.setState({
dataGoal: snap.val(),
currentGoalId: this.props.match.params.goalId,
});
});
}
componentWillUnmount = () => {
this.state = {
currentGoalId: null,
};
}
...
}
我从 React Router V4 链接获得currentGoalId:
<Link title="Open goal" to={"/app/goal/id"+key}>Open goal</Link>
这是一个更高阶的组件。当前 GoalView 组件的 render 方法如下所示:
render() {
return(
<div>
<main className="content">
<p>{this.state.dataGoal.description}</p><br /><br />
{Object.keys(this.state.dataGoal.milestones).map( (milestoneKey) => {
const milestonesData = this.state.dataGoal.milestones[milestoneKey];
return <div className="milestone-wrap" key={milestoneKey}>
<label className="milestone-label truncate">{milestonesData.name}</label>
</div>
})}
</main>
</div>
);
}
}
和国家:
我从这里得到
Cannot convert undefined or null to object行{Object.keys(this.state.dataGoal.milestones).map( (milestoneKey) => {...}
但是该行正上方的渲染方法中的description 渲染得很好。请问大家有什么想法吗?
谢谢你, 雅库布
【问题讨论】:
标签: javascript reactjs firebase firebase-realtime-database