[英]ReactJS/Javascript/Graphql/React-apollo: Cannot read property 'loading' of undefined
I am calling displayElements from the render function and getting this error: TypeError: Cannot read property 'loading' of undefined
.我正在从渲染函数调用 displayElements 并收到此错误: TypeError: Cannot read property 'loading' of undefined
。 Why is this error showing up?为什么会出现这个错误? The reason I think this error is showing up is because here will be no results upon initial rendering.我认为出现此错误的原因是因为初始渲染时没有结果。 But when the submit button is click a query to the DB gets executed and I would like to display the results using the displayElements.但是,当单击提交按钮时,会执行对数据库的查询,我想使用 displayElements 显示结果。
displayElements(){
var data = this.props.getObjectsQuery;
console.log(this.props);
if(data.loading){
return <option disabled>Loading...</option>;
}else{
return data.action.map(action => {
return(<option key={action.action} value={action.timestamp}>{action.filename}</option>);
})
}
}
Submit Button提交按钮
handleSubmit = event => {
event.preventDefault();
console.log(this.state);
this.setState({
startTime: new Date(document.getElementById("startTime").value).valueOf(),//getElementById is a jQuery method
endTime: new Date(document.getElementById("endTime").value).valueOf()
}, () => {
this.props.data.refetch({//Assign the inputvalues, which is the current state, to the variables after pressing the submit button
startTime: this.state.startTime,
endTime:this.state.endTime
});
console.log(this.state.startTime);
console.log(this.state.endTime);
});
};
Write the conditon as if(data)
and then write your code.像if(data)
一样编写条件,然后编写代码。
displayElements(){
var data = this.props.getObjectsQuery;
console.log(this.props);
if(data) {
if(data.loading){
return <option disabled>Loading...</option>;
}else{
return data.action.map(action => {
return(<option key={action.action} value={action.timestamp}>{action.filename}</option>);
})
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.