47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
// API in /api/talas_v5_DB/locationDevice/locationDeviceNameById.js
|
|
import mysql from "mysql";
|
|
|
|
const dbConfig = {
|
|
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,
|
|
};
|
|
|
|
const connection = mysql.createConnection(dbConfig);
|
|
connection.connect((err) => {
|
|
if (err) {
|
|
console.error("Fehler beim Verbinden:", err.stack);
|
|
return;
|
|
}
|
|
});
|
|
|
|
export default async function handler(req, res) {
|
|
if (req.method !== "GET") {
|
|
return res.status(405).json({ error: "Nur GET Methode erlaubt" });
|
|
}
|
|
const { idLD } = req.query;
|
|
|
|
try {
|
|
const query = "SELECT name FROM location_device WHERE idLD = ?";
|
|
const [results] = await new Promise((resolve, reject) => {
|
|
connection.query(query, [idLD], (error, results) => {
|
|
if (error) {
|
|
return reject(error);
|
|
}
|
|
resolve(results);
|
|
});
|
|
});
|
|
|
|
if (results.length > 0) {
|
|
res.json({ name: results[0].name });
|
|
} else {
|
|
res.status(404).json({ error: "Gerät nicht gefunden", idLD, results });
|
|
}
|
|
} catch (error) {
|
|
console.error("Fehler beim Abrufen des Gerätenamens in locationDeviceNameById.js :", error);
|
|
res.status(500).json({ error: "Fehler beim Abrufen des Gerätenamens" });
|
|
}
|
|
}
|