我有一个 Node JS 项目,它从数组中获取数据data2
,使用此数据创建一个对象myObject
使用来自 API 的数据获取let url = `https:...
然后将其保存到 SQL Server 中的数据库中,我遇到的问题是我遵循MS Doc https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15插入数据,但正如我所说here https://stackoverflow.com/questions/70103081/how-can-i-insert-all-my-data-in-sql-using-nodejs-and-tediousMS doc的示例仅添加一个数据,所以我决定重新表述我的问题,如何更改它以插入我的所有数据?
以下是我的连接方式:
var connection = new Connection(config);
connection.on("connect", function (err) {
if (err) {
console.log("Failed to connect!!! ", err);
} else {
console.log("Successful connection");
executeStatement1();
}
});
这是我尝试在循环中插入多个数据的方法:
function executeStatement1() {
const data2 = [
{
latjson: 21.1236,
lonjson: -101.6737,
idoficina: "1",
},
{
latjson: 21.1236,
lonjson: -101.6737,
idoficina: "2",
}
];
for (let item of data2) {
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,
};
const request = new Request(
"EXEC USP_BI_CSL_insert_reg_RegistroTemperaturaXidOdicina @IdOficina, @Humedad, @Nubes, @Sensacion, @Temperatura, @Descripcion",
function (err) {
if (err) {
console.log("No se pudo insertar dato: " + err);
} else {
console.log("Dato con id de Oficina: " + myObject.Id_Oficina + " insertado con éxito.")
}
}
);
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 () {
connection.close();
});
connection.execSql(request);
});
}
}
我遇到的问题是,当我的对象中只有一个对象时,上面的代码会完美插入数据data2
数组,但是当我添加多个数组时,我收到以下错误消息:
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.
在这里,我展示了当我仅使用对象运行代码时,它可以工作:
正如我所说,我决定更改关闭连接的位置,但它也不起作用,只是将我的connection.close();
在我的循环之后:
...
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 () {
connection.close();
});
connection.execSql(request);
}
我希望能够在数组中插入多个数据data2
,如果可能的话,请解释一下为什么我的代码不起作用以及您更改了什么,谢谢!
编辑:我不会放弃哈哈,我继续搜索并在连接时观看:
connection.on("connect", function (err) {
if (err) {
console.log("Failed to connect!!! ", err);
} else {
console.log("Successful connection");
executeStatement1();
}
});
有人告诉我,我只会跑步executeStatement1();
一次,所以它关闭连接,并且不会插入数组的其他部分,但是在循环中执行它会很好,对吗?我想知道关闭连接的正确方法。
我重新表述了我的问题here https://stackoverflow.com/questions/70358386/how-can-i-insert-multiple-times-data-in-sql-server-by-running-the-inserting-func如果您有时间的话,我将不胜感激。