Whenever I run the query to get the holders for a token on base network. Sometimes it comes back successfully and gets displayed on my webiste while other times I get the following error.
Error fetching holders data: TypeError: Cannot read properties of null (reading ‘0’)
at fetchHoldersData (webpack-internal:///(rsc)/./src/app/api/updateHolders/route.ts:54:50)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
Access token: ory_at_H84DGiuTkkNg9PB2NAi2Zk5QZisiiVBJNHqHE4seiqM.VwWnvKpXQ-cE4EBkkPTzaBrim5fcsiq99_3JI-hTFbg
How can I adjust my code so that this no longer happens ?
const fetchHoldersData = async () => {
const access_token = process.env.NEXT_PUBLIC_BIT_QUERY_API_KEY;
const url_graphql = "https://streaming.bitquery.io/graphql";
const currentDate = new Date().toISOString().slice(0, 10);
const query = `
{
EVM(dataset: archive, network: base) {
TokenHolders(
date: "${currentDate}"
tokenSmartContract: "0x9a26F5433671751C3276a065f57e5a02D2817973"
where: { Balance: { Amount: { gt: "0" } } }
) {
uniq(of: Holder_Address)
}
}
}
`;
try {
const response = await fetch(url_graphql, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`,
},
body: JSON.stringify({ query }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
holdersCount = data.data.EVM.TokenHolders[0].uniq;
console.log("Holders count updated:", holdersCount);
} catch (error) {
console.error('Error fetching holders data:', error);
console.log("Access token:", process.env.NEXT_PUBLIC_BIT_QUERY_API_KEY);
}
};