I have a NodeJS project that saves data to a Database using a Stored Procedure, I builded this code asking here and watching Documentation from Microsoft the issue is that everytime I run my code using node app.mjs
I get the same messages errors:
Successful connection
Couldn't insert data: Error: Requests can only be made in the LoggedIn state, not the SentClientRequest state
Couldn't insert data: Error: Connection closed before request completed.
I searched here and read some documentation, I change my code of this question but either worked. I want to fix both errors, as you can see everything in my connection works fine, it is just that I can't execute my SP. Here is my code of how I do my connection and run my function:
connection.on("connect", function (err) {
if (err) {
console.log("Failed to connect!!! ", err);
} else {
console.log("Successful connection");
calcWeather();
}
});
connection.connect();
and here's how I'm trying to save data, saving it to an object and later using a request
from tedious
:
function calcWeather() {
const data = [
{
latjson: 21.1236,
lonjson: -101.6737,
idoficina: "3",
},
{
latjson: 21.8818,
lonjson: -102.2911,
idoficina: "4",
},
];
for (let item of data) {
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${item.latjson}&lon=${item.lonjson}&appid=${api_key}&units=metric&lang=sp`;
fetch(url)
.then((response) => {
return response.json();
})
.then(function (data) {
var myObject = {
Id_Oficina: item.idoficina,
Humedad: data.main.humidity,
Nubes: data.clouds.all,
Sensacion: data.main.feels_like,
Temperatura: data.main.temp,
Descripcion: data.weather[0].description,
};
// validation
if (myObject.Temperatura < 99) {
const request = new Request(
"EXEC USP_BI_CSL_insert_reg_RegistroTemperaturaXidOdicina @IdOficina, @Humedad, @Nubes, @Sensacion, @Temperatura, @Descripcion",
function (err) {
if (err) {
console.log("Couldn't insert data: " + err);
}
}
);
request.addParameter("IdOficina", TYPES.SmallInt, myObject.Id_Oficina);
request.addParameter("Humedad", TYPES.SmallInt, myObject.Humedad);
request.addParameter("Nubes", TYPES.SmallInt, myObject.Nubes);
request.addParameter("Sensacion", TYPES.Float, myObject.Sensacion);
request.addParameter("Temperatura", TYPES.Float, myObject.Temperatura);
request.addParameter("Descripcion", TYPES.VarChar, myObject.Descripcion);
request.on("row", function (columns) {
columns.forEach(function (column) {
if (column.value === null) {
console.log("NULL");
} else {
console.log("Product id of inserted item is " + column.value);
}
});
});
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.