Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { NextResponse } from 'next/server';
- import pool from '../../../../lib/db';
- import mysql, { RowDataPacket } from 'mysql2/promise';
- export async function GET() {
- let connection: mysql.PoolConnection | undefined;
- try {
- connection = await pool.getConnection();
- const [rows]: [RowDataPacket[], any] = await connection.query('SELECT COUNT(*) as count FROM guild');
- const count = rows[0].count;
- await connection.release();
- return NextResponse.json({ count }, { status: 200 });
- } catch (error) {
- console.error('Error fetching data:', error); // Log the error for debugging
- if (connection) {
- connection.release();
- return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
- }
- } finally {
- if (connection) {
- try {
- await connection.release(); // Release connection back to the pool
- console.log("Released")
- } catch (releaseError) {
- console.error('Error releasing connection:', releaseError); // Log release error
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment