I'm new to ReactJs, this is the code for fetching data from truepush api.
export const getAllCampaign = () => {
return fetch(`https://api.truepush.com/api/v1/listCampaign/1`, {
method: "GET",
headers: {
Authorization: `${TOKEN}`,
"Content-Type": "application/json",
},
})
.then(response => {
return response.json()
})
.catch(err => console.log(err))
}
This api has limit, once the limit gets exceeds it shows an error of "campaigns.map not a function" at this place :
const allData = campaigns.map((campaign, i) => ({
...campaign,
...stats[i].data
}));
Here is my full code :
import React, {useState, useEffect} from 'react';
import {getAllCampaign, loadStats} from "../helpers/coreapihelpers";
const TableRow = () => {
const [campaigns, setCampaigns] = useState([]);
const [stats, setStats] = useState([]);
const loadAllCampaigns = () => {
getAllCampaign()
.then(data => { setCampaigns(data.data) })
.catch(err => { console.log(err) });
};
const loadAllStats = () => {
loadStats()
.then(data => { setStats(data) })
.catch(err => { console.log(err) });
}
useEffect(() => {
loadAllCampaigns();
loadAllStats();
}, [])
const allData = campaigns.map((campaign, i) => ({
...campaign,
...stats[i].data
}));
console.log(allData);
return (
<div className="container">
<div className="row">
<div className="col-xs-12">
<div className="table-responsive" data-pattern="priority-columns">
<table className="table table-bordered table-hover">
<thead>
<tr>
<th>Sr. No</th>
<th>Campaign Id</th>
<th>Campaign Name</th>
<th>Campaign Status</th>
<th>Reach</th>
<th>Sent</th>
<th>Delivered</th>
<th>Views</th>
<th>Clicks</th>
<th>Unsubscribers</th>
</tr>
</thead>
<tbody>
{allData.map(
(
{
campaignId,
campaignTitle,
campaignStatus,
Reach,
Sent,
Delivered,
Views,
Clicks,
Unsubscribers
},
index
) => (
<tr key={index}>
<td>{index + 1}</td>
<td>{campaignId}</td>
<td>{campaignTitle}</td>
<td>{campaignStatus}</td>
<td>{Reach}</td>
<td>{Sent}</td>
<td>{Delivered}</td>
<td>{Views}</td>
<td>{Clicks}</td>
<td>{Unsubscribers}</td>
</tr>
)
)}
</tbody>
</table>
</div>
</div>
</div>
</div>
);
}
export default TableRow;
How can i show a simple h1 tag saying limit exceeds when api limit exceeds or fails to fetch data ?
question from:
https://stackoverflow.com/questions/65842467/error-handling-while-api-limit-exceeds-in-reactjs 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…