add: locationDeviceNameById.js
This commit is contained in:
39
pages/api/locationDeviceNameById.js
Normal file
39
pages/api/locationDeviceNameById.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
// API in /api/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 function handler(req, res) {
|
||||||
|
if (req.method !== "GET") {
|
||||||
|
return res.status(405).json({ error: "Nur GET Methode erlaubt" });
|
||||||
|
}
|
||||||
|
const { idLD } = req.query;
|
||||||
|
|
||||||
|
const query = "SELECT name FROM location_device WHERE idLD = ?";
|
||||||
|
connection.query(query, [idLD], (error, results) => {
|
||||||
|
if (error) {
|
||||||
|
console.error("Fehler beim Abrufen des Gerätenamens:", error);
|
||||||
|
return res.status(500).json({ error: "Fehler beim Abrufen des Gerätenamens" });
|
||||||
|
}
|
||||||
|
if (results.length > 0) {
|
||||||
|
res.json({ name: results[0].name });
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ error: "Gerät nicht gefunden" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user