【发布时间】:2020-04-02 02:18:00
【问题描述】:
我尝试在前面使用 API 中显示数据,但在浏览器控制台中出现此错误状态并且数据未显示:
Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined 在 eval (tickets.jsx:190)
这就是我在 render() 中显示数据的方式:
render(){
var { items_tickets, dd_tickets, role, filter_tickets, all_tickets } = this.state;
return(
<table className="table table-striped table-dark table-hover">
<thead>
<tr>
<th> N° de Ticket </th>
<th> Mes Gains </th>
<th> (Non) utilisé </th>
</tr>
</thead>
<tbody>
{
filter_tickets.length > 0 ? filter_tickets.map(item => {
console.log('filter tickets : ', filter_tickets)
const {numTicket, libelleGain, used} = item;
// console.log("items_tickets : ", items_tickets)
return <tr key={numTicket}>
<td>{numTicket}</td>
<td>{libelleGain}</td>
<td>{used === true ? "Utilisé" : "Non utilisé"}</td>
</tr>
}) : "d"
}
</tbody>
</table>
</div>
</div>
)
}
这是我前面的API函数:
// Get all tickets
filterallTickets(){
const head = localStorage.getItem('head');
const payload = localStorage.getItem('payload');
const signature = localStorage.getItem('signature');
var tok = head + "." + payload + "." + signature;
// Gains List
fetch(`${API}/api/tickets/getalltickets`,{
method: 'GET',
headers :{
'Content-Type': 'application/json',
'authorization': `Bearer ${tok}`, } })
.then(results => {
return results.json();
})
.then(res => {
console.log("res : ",res);
if(res.success === true){
this.setState({ filter_tickets: res.result });
// this.setState({items_tickets: []});
}
})
}
谢谢
【问题讨论】:
标签: reactjs fetch state render setstate