Files
nodeMap/pages/api/talas_v5_DB/locationDevice/locationDeviceNameById.js
2024-09-02 09:06:49 +02:00

37 lines
1.1 KiB
JavaScript

// API in /api/talas_v5_DB/locationDevice/locationDeviceNameById.js
import getPool from "../../../../utils/mysqlPool"; // Singleton-Pool importieren
export default async function handler(req, res) {
const pool = getPool(); // Singleton-Pool verwenden
if (req.method !== "GET") {
return res.status(405).json({ error: "Nur GET Methode erlaubt" });
}
const { idLD } = req.query;
if (!idLD) {
return res.status(400).json({ error: "idLD ist erforderlich" });
}
let connection;
try {
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
const query = "SELECT name FROM location_device WHERE idLD = ?";
const [results] = await connection.query(query, [idLD]);
if (results.length > 0) {
res.status(200).json({ name: results[0].name });
} else {
res.status(404).json({ error: "Gerät nicht gefunden", idLD });
}
} catch (error) {
console.error("Fehler beim Abrufen des Gerätenamens:", error);
res.status(500).json({ error: "Fehler beim Abrufen des Gerätenamens" });
} finally {
if (connection) connection.release(); // Gib die Verbindung in den Pool zurück
}
}