I have a node express application being hosted on Render which has a /test route. In this route i do the following:
Retrieve some object from my MongoDB hosted in Mongo Atlas
Create a function that uses a for loop to loop through the object and push urls into an array.
A second function that takes that url array and fetches data and pushes it into a data array
app.get(“/test”, async (req, res) => {
// START OF GET URLS FROM DB
const dbBrands = await Brand.find({});
const getUrlForLoop = (obj) => {
let data = ;
for (const el of obj) {
let url = el.url;
data.push(url);
}
return data;
};
const urls = getUrlForLoop(dbBrands);
// END OF GET URLS FROM DB
// START OF GET DATA FROM URL FOR LOOP
const getDataForLoop = async (urls) => {
let data = ;
for (const url of urls) {
let response = await fetch(url);
let resData = await response.json();
data.push(resData);
}
return data;
};
const fetchedData = await getDataForLoop(urls);
res.send(fetchedData);
// END OF GET DATA FROM URL FOR LOOP
});
I’m using Postman to test the API calls.
When my express server is running locally on my machine and i hit the route, i get the data as expected.
However when i try to hit the route that is being hosted on Render, it fails with the below error:
I’ve tested with fetching one url on its own and the data gets retrieved from the fetch as expected so it seems like its an issue during the looping through the urls.
Running my express backend on my localhost I have no timeout issue when fetching the data but when running my express backend on render the fetch request times out.