Trying to fetch data from a MySQL database using Next.js API routes, I encountered the following error:
Error: No response returned from the route handler
Below is an example of the code being used:
import { NextResponse } from "next/server";
import db from '../../../../lib/db';
export function GET(request){
db.query('SELECT * FROM department', (error, results, fields) => {
if (error) {
console.error('Error querying the database:', error);
return NextResponse.json({ error: 'Internal Server Error' },{status:500});
}
return NextResponse.json({ data: results },{status:201});
});
}
Upon calling this API in the console, I first see the message "Connected to MySQL database successfully" followed by the aforementioned error.