Stationnamen sind richtig
This commit is contained in:
43
pages/api/talas_v5_DB/station/getAllStationsNames.js
Normal file
43
pages/api/talas_v5_DB/station/getAllStationsNames.js
Normal file
@@ -0,0 +1,43 @@
|
||||
// /pages/api/talas_v5_DB/station/getAllStationsNames.js
|
||||
import mysql from "mysql2/promise";
|
||||
|
||||
// Verbindungspool-Konfiguration
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
port: process.env.DB_PORT,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== "GET") {
|
||||
res.setHeader("Allow", ["GET"]);
|
||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
|
||||
try {
|
||||
// Abrufen aller idLD und ihrer Namen
|
||||
const [results] = await pool.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" });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user