i'm using geocode api which have 10 request per second with in gatsby, i'm creating pages during develop
, inside map function i'm calling geocode api to get the lat
,lon
from address to pass it to context
for each page
but i'm hitting api limit 10 request per second, tried with enter link description here
but i think i'm doing it wrong, because page creation failed
"gatsby-node.js" threw an error while running the createPages lifecycle:
const getAllData = async () =>
Promise.all(
//data from graphql
data.map(async (node) => {
//googleapi function send fetch request to api
const geo = await limiter.schedule(() => googleapi({ address }));
results = await anotherapi_base_on_res(res.latitude, res.longitude);
return {
path: `/${slug}`,
component: require.resolve(`./src/templates/abc.js`),
context: {
slug: node.url,
},
};
})
);
const dataResult = await getAllData();
pages are not being created, is it the right way to use Bottleneck
Pages are created when you run the
gatsby develop
orgatsby build
commands, on runtime, once. I'm afraid this approach will never work as is.More details about
createPage
API:There are a lot of implementations detail lacking, but I think I get what you are trying to do. I think your code should look like:
The idea, once you get your data (I assume is a GraphQL query) is to loop through it but using a
forEach
, not amap
since you don't want to return an array, you just want tocreatePage
for each node, as Gatsby docs points out.However, using a
forEach
you won't be able to use anasync
modifier, so you will need to change your workaround/approach to call anasync
function externally with the needed data (someFancyFunction
), assuming it returns alatitude
andlongitude
variables (tweak it accordingly). After that, can pass your data via context into your template.Another workaround would be doing the loop to get the coordinates before the
forEach
(using anasync
map
if needed or afor..of
, etc.) to set an array of coordinates, then you can provide that data to the template for the corresponding position.