일부 아약스 요청으로 일부 데이터를 읽으려고했지만 코드가 올바른지 확실하지 않습니다.Reactjs에서 정의되지 않은 속성을 읽을 수 없습니다.
interface IProps {
data: IScheduler;
}
interface IState {
list: IScheduler;
}
export default class Page extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
list: this.props.data,
};
}
public componentWillMount() {
this.getData();
}
public getData =() => {
axios.get(myURL)
.then((response) => {
this.setState({list: response.data});
console.log(response.data.generatedDate); //works
})
.catch((error) => {
console.log("DIDN'T CATCH DATA", error);
});
}
public render(): JSX.Element {
const data: IScheduler = this.state.list;
// getting an error
console.log(data.creationDate);
return (
<div className="container">
<MyListCmp data={data}/>
</div>
);
}
내가 오류를 참조하십시오 :
Uncaught TypeError: Cannot read property 'list' of undefined
내가 시도 :
console.log(data); // or this.state.list
그냥 정의의를 여기에 코드입니다.
데이터 예 :
Uncaught TypeError: Cannot read property 'generatedDate' of undefined
Uncaught TypeError: Cannot read property 'meetings' of undefined
가 어떻게 그것을 읽을 수 있습니다 : 나는 '회의'또는 'generatedDate을'읽고 싶은 때 같은 오류가 MyListCmp에서 발생
{
"generatedDate": "12.12.2017",
"meetings": [
...
]
}
? render() 또는 return()에서 아직 데이터를 가져 오지 않았습니까? 내가 말할 수있는
그 I는 다음과 같이 할 경우 :
const data: IScheduler = {
generatedDate: "12.12.2017",
meetings: [
...
]
}
그것은 작동하지만,이 시점이 아니다. 일부 URL에서 가져와야합니다. 모든 조언을 부탁드립니다.
생성자에서'this.getData'를 바인딩 해보십시오. 생성자'this.getData = this.getData.bind (this) '에 이것을 추가함으로써 –
@DhavalJardosh, 고맙지 만 도움이되지 않았다. –