.env.local für 30
This commit is contained in:
39
pages/api/talas_v5_DB/device/getAllStationsNames.js
Normal file
39
pages/api/talas_v5_DB/device/getAllStationsNames.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// /pages/api/talas_v5_DB/device/getAllStationsNames.js
|
||||
import getPool from "../../../../utils/mysqlPool"; // Importiere den Singleton-Pool
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const pool = getPool(); // Verwende den Singleton-Pool
|
||||
|
||||
if (req.method !== "GET") {
|
||||
res.setHeader("Allow", ["GET"]);
|
||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
|
||||
let connection;
|
||||
|
||||
try {
|
||||
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
|
||||
|
||||
// Abrufen aller idLD und ihrer Namen
|
||||
const [results] = await connection.query("SELECT idLD, name FROM location_device");
|
||||
|
||||
if (results.length === 0) {
|
||||
return res.status(404).json({ error: "No data found" });
|
||||
}
|
||||
|
||||
// Struktur der Antwort anpassen
|
||||
const namesMap = results.reduce((map, { idLD, name }) => {
|
||||
if (!map[idLD]) {
|
||||
map[idLD] = name; // Stelle sicher, dass hier keine Duplikate oder Überschreibungen entstehen
|
||||
}
|
||||
return map;
|
||||
}, {});
|
||||
|
||||
res.status(200).json(namesMap);
|
||||
} catch (err) {
|
||||
console.error("Fehler beim Abrufen der Daten:", err);
|
||||
res.status(500).json({ error: "Error retrieving data from the database" });
|
||||
} finally {
|
||||
if (connection) connection.release(); // Gib die Verbindung zurück in den Pool
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user