I am configuring a panel that is capable of querying alerts from the database with an ID and returning the results of those alerts on demand as requested by the client. However, I have two problems. The first is that I am unable to establish the connection between the panel and the database; the function for on-demand queries is not running. The second is that I am testing locally to ensure that the alerts that should appear on the panel are correctly configured. If the form is not filled out correctly, I have the following in the initial request to display error messages:
function cdfFunction(session, id_num, station_num, familia_activo_num, unidad_num) {
// Verification of conditions
if ((familia_activo_num.toLowerCase() === 'variador') && (['701', '702', '703'].includes(unidad_num))) {
handleVariadorError();
return; // Stop execution
}
if (id_num && (station_num || familia_activo_num || unidad_num)) {
handleIDAlertaError();
return; // Stop execution
}
// Main logic
fetch(/* ... */)
.then((resp) => {
if (resp.ok) {
notifySuccess(["Update", "Values updated successfully."]);
initialRequest();
} else {
notifyError(["Error", `An error occurred updating values: ${resp.status}`]);
}
})
.catch((error) => {
console.error(error);
notifyError(["Error", "An unexpected error occurred."]);
});
}
// Error messages
function handleVariadorError() {
notifyError(["Error", "Units 701, 702, and 703 do not have a variator."]);
}
function handleIDAlertaError() {
notifyError(["Error", "Please enter only the alert ID."]);
}
On the other hand, in the update request I have the following: function cdfFunction(session, id_num, station_num, familia_activo_num, unidad_num) {
const nonce = session;
const url = ‘http://services.repcenter.skf.com:3000/api/datasources/proxy/uid/gosBqEF4z/cdf-cc-oauth/api/v1/projects/skfcenit/functions/525789850044078/call’;
const id_NumInt = parseInt(id_num, 10);
const station_Num = station_num.toString();
const familia_activo_Num = familia_activo_num.toString();
const unidad_NumInt = parseInt(unidad_num, 10);
const requestBody = {
data: {
“id de alerta”: id_alertaNumInt,
“station”: station_str,
“Familia de activo”: familia_del_activo_str,
“unidad de bombeo”: Unidad_str,
},
nonce: nonce
};
const resp = fetch(url, {
method: “POST”,
headers: { “content-type”: “application/json” },
body: JSON.stringify(requestBody)
})
.catch((error) => {
console.error(error);
})
.then((resp) => {
console.log(resp);
});
}
function session() {
const body = {};
options.elements.forEach((element) => {
if (!options.update.updatedOnly) {
body[element.id] = element.value;
return;
}
if (element.value === initial[element.id]) {
return;
}
body[element.id] = element.value;
});
var id_num = body[“id de alerta”];
var station_num = body[“station”];
var familia_activo_num = body[“Familia de activo”];
var unidad_num = body[“unidad de bombeo”];
const session = ‘http://services.repcenter.skf.com:3000/api/datasources/proxy/uid/gosBqEF4z/cdf-cc-oauth/api/v1/projects/skfcenit/sessions’;
const resp = fetch(session, {
method: “POST”,
headers: { “content-type”: “application/json” },
body: JSON.stringify({
“items”: [{
“clientId”: “”,
“clientSecret”: “”
}]
})
})
.then(resp => resp.json())
.then(result => {
const nonce = result.items[0].nonce;
cdfFunction(nonce, id_num, station_num, familia_activo_num, unidad_num);
// Clear values of specific elements
options.elements.forEach((element) => {
if (element.id === "id_alerta" || element.id === "station") {
element.value = "";
}
});
// Update options
onOptionsChange({
...options,
elements: options.elements.map((element) => {
return (element.id === "id_alerta" || element.id === "station") ? { ...element, value: "" } : element;
}),
});
})
.catch(error => console.log('error', error));
}
session();
Please, I need your help to detect the errors that are preventing the connection or, in the local environment, not displaying the messages as intended."