I’m stuck with a Node and TypeScript project. I’m trying to fetch data from Airtable using their API. The weird thing is, I can see all the data in my backend console, but when I try to get it through Postman or my frontend, I get nothing. The request just times out.
I’ve tried everything I can think of. I’ve messed around with ‘return’ statements, tried using Promises, and even async/await. But no luck so far.
Here’s a simplified version of my code:
import { AirtableBase } from 'airtable';
const fetchStudentInfo = async (studentName: string): Promise<any> => {
const db: AirtableBase = initializeAirtableConnection();
return new Promise((resolve, reject) => {
db('Students')
.select({ filterByFormula: `{Name} = "${studentName}"` })
.firstPage((err, records) => {
if (err) {
reject(err);
return;
}
resolve(records.map(record => ({ id: record.id, ...record.fields })));
});
});
};
app.get('/student-info', async (req, res) => {
try {
const data = await fetchStudentInfo('Alice');
res.json(data);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch student info' });
}
});
When I hit the ‘/student-info’ endpoint in Postman, I get a ‘socket hang up’ error. Any ideas what I’m doing wrong here? I’m totally stumped!
Have you checked your CORS configuration? It sounds like the data is reaching your server but not making it to the client. Make sure you’ve set up CORS correctly in your Express app:
import cors from 'cors';
app.use(cors());
Also, consider adding error logging in your catch block to see if any errors are being swallowed:
catch (error) {
console.error('Error fetching student info:', error);
res.status(500).json({ error: 'Failed to fetch student info' });
}
Lastly, verify that your Airtable rate limits haven’t been exceeded. They have strict limits that can cause issues if you’re making frequent requests. You might need to implement some request throttling on your end.
I’ve encountered a similar issue with Airtable API in the past. The problem might be related to how you’re handling the response and closing the connection.
Try modifying your code to explicitly end the response after sending the data:
app.get('/student-info', async (req, res) => {
try {
const data = await fetchStudentInfo('Alice');
res.json(data);
res.end();
} catch (error) {
res.status(500).json({ error: 'Failed to fetch student info' });
res.end();
}
});
Also, ensure your server isn’t timing out before the Airtable API responds. You might want to increase the timeout limit:
const server = app.listen(port, () => {
server.timeout = 120000; // Set timeout to 2 minutes
console.log(`Server running on port ${port}`);
});
If these don’t work, double-check your Airtable API key and base ID. Sometimes, authentication issues can cause silent failures. Hope this helps!
hey man, have u tried using async/await instead of Promises? sometimes that can help. also, maybe check ur network tab in devtools to see if there’s any errors. could be a CORS issue or sumthin. oh and make sure ur airtable API key is still valid, that tripped me up once lol