Stationnamen sind richtig

This commit is contained in:
ISA
2024-08-10 14:06:19 +02:00
parent da487ba7bb
commit d729e1408b
3 changed files with 46 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
// /pages/api/talas_v5_DB/station/getStationNameByIdLD.js
// /pages/api/talas_v5_DB/station/getAllStationsNames.js
import mysql from "mysql2/promise";
// Verbindungspool-Konfiguration
@@ -20,10 +20,22 @@ export default async function handler(req, res) {
}
try {
// Verwenden des Verbindungspools, um die Abfrage auszuführen
const [results] = await pool.query("SELECT * FROM location_device");
// Abrufen aller idLD und ihrer Namen
const [results] = await pool.query("SELECT idLD, name FROM location_device");
res.status(200).json(results);
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" });