请求只能在 LoggedIn 状态下进行,不能在 Connecting 状态下进行,
Requests can only be made in the LoggedIn state, not the Connecting state,
我正在开发一个插入对象数组的 NodeJS 项目,然后将这些对象插入到 SQL 服务器中的数据库中,它应该从数组的位置连接并插入数据 lstValid[1]
但我收到此错误:
Couldn't insert data: Error: Requests can only be made in the LoggedIn state, not the Connecting state
(node:9560) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined
这是我的app.js
var Connection = require("tedious").Connection;
var lstValid = [];
var config = {
server: "***",
authentication: {
type: "default",
options: {
userName: "sa", password: "***",
},
},
options: {
encrypt: true,
database: "***",
},
};
var connection = new Connection(config);
connection.on("connect", function (err) {
console.log("Successful connection");
executeStatement1();
});
connection.connect();
const api_key = "*****";
async function calcWeather() {
const info = await fetch("../json/data.json") // bringing data.json with all data
.then(function (response) {
return response.json();
});
for (var i in info) {
const _idOficina = info[i][0].IdOficina;
const lat = info[i][0].latjson;
const long = info[i][0].lonjson;
const base = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${api_key}&units=metric&lang=sp`;
fetch(base)
.then((responses) => {
return responses.json();
})
.then((data) => {
var myObject = {
Id_Oficina: _idOficina,
// Some other stuff from the object
};
// validation and saving data to array
if (myObject.Temperatura < 99) {
lstValid.push(myObject);
} else if (myObject.Temperatura > 99) {
lstUnValid.push(myObject);
}
});
}
}
var Request = require("tedious").Request;
var TYPES = require("tedious").TYPES;
function executeStatement1() {
calcWeather();
request = new Request(
"INSERT INTO TB_BI_CSL_RegistroTemperaturaXidOdicina (IdOficina, Humedad, Nubes, Sensacion, Temperatura, Descripcion) VALUES (@IdOficina, @Humedad, @Nubes, @Sensacion, @Temperatura)",
function (err) {
if (err) {
console.log("Couldn't insert data: " + err);
}
}
);
request.addParameter("IdOficina", TYPES.SmallInt, lstValid[1]);
// Some other data inserted
request.on("row", function (columns) {
columns.forEach(function (column) {
if (column.value === null) {
console.log("NULL");
} else {
console.log("Inserted value");
}
});
});
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
尝试检查并报告连接错误,即:
connection.on("connect", function (err) {
if(err) {
console.log('Error: ', err)
} else {
console.log("Successful connection");
executeStatement1();
}
});
我正在开发一个插入对象数组的 NodeJS 项目,然后将这些对象插入到 SQL 服务器中的数据库中,它应该从数组的位置连接并插入数据 lstValid[1]
但我收到此错误:
Couldn't insert data: Error: Requests can only be made in the LoggedIn state, not the Connecting state
(node:9560) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined
这是我的app.js
var Connection = require("tedious").Connection;
var lstValid = [];
var config = {
server: "***",
authentication: {
type: "default",
options: {
userName: "sa", password: "***",
},
},
options: {
encrypt: true,
database: "***",
},
};
var connection = new Connection(config);
connection.on("connect", function (err) {
console.log("Successful connection");
executeStatement1();
});
connection.connect();
const api_key = "*****";
async function calcWeather() {
const info = await fetch("../json/data.json") // bringing data.json with all data
.then(function (response) {
return response.json();
});
for (var i in info) {
const _idOficina = info[i][0].IdOficina;
const lat = info[i][0].latjson;
const long = info[i][0].lonjson;
const base = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${api_key}&units=metric&lang=sp`;
fetch(base)
.then((responses) => {
return responses.json();
})
.then((data) => {
var myObject = {
Id_Oficina: _idOficina,
// Some other stuff from the object
};
// validation and saving data to array
if (myObject.Temperatura < 99) {
lstValid.push(myObject);
} else if (myObject.Temperatura > 99) {
lstUnValid.push(myObject);
}
});
}
}
var Request = require("tedious").Request;
var TYPES = require("tedious").TYPES;
function executeStatement1() {
calcWeather();
request = new Request(
"INSERT INTO TB_BI_CSL_RegistroTemperaturaXidOdicina (IdOficina, Humedad, Nubes, Sensacion, Temperatura, Descripcion) VALUES (@IdOficina, @Humedad, @Nubes, @Sensacion, @Temperatura)",
function (err) {
if (err) {
console.log("Couldn't insert data: " + err);
}
}
);
request.addParameter("IdOficina", TYPES.SmallInt, lstValid[1]);
// Some other data inserted
request.on("row", function (columns) {
columns.forEach(function (column) {
if (column.value === null) {
console.log("NULL");
} else {
console.log("Inserted value");
}
});
});
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
尝试检查并报告连接错误,即:
connection.on("connect", function (err) {
if(err) {
console.log('Error: ', err)
} else {
console.log("Successful connection");
executeStatement1();
}
});