Home
Support Forums

502 bad gateway with function

Here is the function: Netlify App

It was running fine locally, but after the netlify deploy it’s throwing this error. No issues reported during the build process.

Hi @caleb

Where are you using this function? How is it called?
Can you share the function code?

Here is the courses.js function code:

const formattedReturn = require('./helpers/formattedReturn');
const getCourses = require('./helpers/getCourses');
const createCourse = require('./helpers/createCourse');
const deleteCourse = require('./helpers/deleteCourse');
const updateCourse = require('./helpers/updateCourse');
exports.handler = async (event) => {
    if (event.httpMethod === 'GET') {
        return await getCourses(event);
    } else if (event.httpMethod === 'POST') {
        return await createCourse(event);
    } else if (event.httpMethod === 'PUT') {
        return await updateCourse(event);
    } else if (event.httpMethod === 'DELETE') {
        return await deleteCourse(event);
    } else {
        return formattedReturn(405, {});
    }
}; 

getCourses.js:
const { table } = require('./airtable');
const formattedReturn = require('./formattedReturn');
module.exports = async (event) => {
    const courses = await table.select().firstPage();
    const formattedCourses = courses.map((course) => ({
        id: course.id,
        ...course.fields,
    }));
    return formattedReturn(200, formattedCourses);
};

Here is the call:
function App() {
const [courses, setCourses] = useState();

const loadCourses = async () => {
    try {
        const res = await fetch('/.netlify/functions/courses');
        const courses = await res.json();
        setCourses(courses);
    } catch (error) {
        console.error(error);
    }
};

Thank you for the help.

I believe you might not need to use return and await in a single statement. What happens if you remove await, like: return await getCourses(event);?

I removed await from courses.js, but still getting the 502 error.

Alright then, moving on, are you sure your export matches the require syntax that you’ve used here:

You might want to share airtable file’s code for us to be able to guess. If it’s not that, looking at the error: SyntaxError: Missing initializer in destructuring declaration, it might be some other stuff that you’re trying to descrutrue, but in the code that you’ve shared, this seems the only possible place.

Here is the airtable code:

require('dotenv').config();
const Airtable = require('airtable');
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(
    process.env.AIRTABLE_BASE_ID
);
const table = base(process.env.AIRTABLE_TABLE_NAME);

module.exports = { table };

While there’s nothing obvious that stands out as a problem from the mentioned pieces of code, would you be able to add additional log statements in the code to help us and in turn you see the problem?

After almost each line, you could try to add a console.log to see till what part code executes successfully and when exactly it fails.