I'm trying to save specific data from a WEB API to my MySql Database using Node JS, but I'm having trouble.
Here is my API.
I need to get the following values from each device.
- ID
- Name
- Type
- Enabled
- Value
So far I've been trying to use the following code, but it seems to only work for the last ID? I'm counting if the Device ID is already in my Database, if not it'll add the device along with all data. Otherwise, it'll output the ID along with the Value (and update the corresponding data within the database). My goal is to request the API every few seconds, run through each device id and update my database. This is the only solution to what I'm trying to achieve and so far it's just giving me a headache. Massive thanks to anyone who knows how to achieve this or what I'm doing wrong.
fetch('http://127.0.0.1:3000/test', settings)
.then(res => res.json())
.then((json) => {
for (var i = 0; i < json.length; i++) {
json.id = json[i].id;
json.value = json[i].properties.value;
json.name = json[i].name;
json.type = json[i].type;
json.enabled = json[i].enabled;
connection.query('SELECT COUNT(*) AS exist FROM devices WHERE id = ? AND state = ?;', [json.id, "enabled"], function(error, rows, fields) {
if (error) {
console.log(error);
}
if (rows[0].exist == 0) {
connection.query('INSERT INTO devices(id, name, value, type, state) VALUES( ? , ? , ? , ? );', [json.id, json.name, json.value, json.type, json.enabled], function(error, rows, fields) {
if (error) {
console.log(error);
}
});
} else if (rows[0].exist == 1) {
console.log(json.id + ";" + json.value);
}
});
};
});
let i = 0;insteadvar i = 0;