为什么在尝试使用 NodeJS 和 Tedious 将数据插入 SQL 服务器时出现此错误?
Why do I get this error while trying to insert data to SQL Server with NodeJS and Tedious?
在将数据保存到 SQL 服务器数据库的 NodeJS 中工作,它必须从对象数组中保存数据但是当我 运行 它时我得到这个错误,只是看这里和文档但我真的不明白如何解决它,欢迎任何帮助。这是错误:
PS D:\Users\****\****\****\****\****> node appb.js
Successful connection
events.js:135
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received type string ('row')
这是我的 app.js:
连接:
var Connection = require("tedious").Connection;
var lstValid = [];
var config = {
server: "SERVER",
authentication: {
type: "default",
options: {
userName: "USERNAME",
password: "PASSWORD",
},
},
options: {
encrypt: true,
database: "DATABASE",
instanceName: 'INSTANCENAME'
},
};
var connection = new Connection(config);
connection.on("connect", function (err) {
console.log("Successful connection");
executeStatement1();
});
connection.connect();
这里是我插入数据的地方:
async function calcWeather() {
const info = await fetch("../json/data.json")
.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,
// Other thins in myObject
};
// validation and saving data to array
if (myObject.Temperatura < 99) {
lstValid.push(myObject);
}
});
}
}
var Request = require("tedious").Request;
var TYPES = require("tedious").TYPES;
function executeStatement1() {
calcWeather();
for (var m = 0; m <= lstValid.length; m++) {
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[m]);
// Other things inserted
Request.on('requestCompleted',"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);
}
}
似乎 request.on(...)
方法的参数太多,即:
Request.on('requestCompleted',"row", function (columns)
大概应该是:
Request.on("row", function (columns)
错误表明 JavaScript 函数收到的参数与预期的不同:
...ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
如果它从未起作用,则该函数可能输入错误。 (如果有效,则可能是输入了错误的数据)
下一条消息提供了进一步的信息:
"...The "listener" argument must be of type function. Received type string ('row')"
需要一个 JavaScript 函数来完成工作,但它收到了一个简单的字符串 'row'。
events.js:135
这意味着错误发生在文件 'events.js' 的第 135 行或之前。
TediusJs API Request Docs,提供了参考例子:
request.on('row', function (columns) { /* code to process rows */ });
在您的示例中,我们发现:
Request.on('requestCompleted',"row", function (columns) {
很可能应该是:
Request.on("row", function (columns) {
虽然我不确定你的示例中的哪一行是第 135 行。
在将数据保存到 SQL 服务器数据库的 NodeJS 中工作,它必须从对象数组中保存数据但是当我 运行 它时我得到这个错误,只是看这里和文档但我真的不明白如何解决它,欢迎任何帮助。这是错误:
PS D:\Users\****\****\****\****\****> node appb.js
Successful connection
events.js:135
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received type string ('row')
这是我的 app.js:
连接:
var Connection = require("tedious").Connection;
var lstValid = [];
var config = {
server: "SERVER",
authentication: {
type: "default",
options: {
userName: "USERNAME",
password: "PASSWORD",
},
},
options: {
encrypt: true,
database: "DATABASE",
instanceName: 'INSTANCENAME'
},
};
var connection = new Connection(config);
connection.on("connect", function (err) {
console.log("Successful connection");
executeStatement1();
});
connection.connect();
这里是我插入数据的地方:
async function calcWeather() {
const info = await fetch("../json/data.json")
.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,
// Other thins in myObject
};
// validation and saving data to array
if (myObject.Temperatura < 99) {
lstValid.push(myObject);
}
});
}
}
var Request = require("tedious").Request;
var TYPES = require("tedious").TYPES;
function executeStatement1() {
calcWeather();
for (var m = 0; m <= lstValid.length; m++) {
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[m]);
// Other things inserted
Request.on('requestCompleted',"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);
}
}
似乎 request.on(...)
方法的参数太多,即:
Request.on('requestCompleted',"row", function (columns)
大概应该是:
Request.on("row", function (columns)
错误表明 JavaScript 函数收到的参数与预期的不同:
...ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
如果它从未起作用,则该函数可能输入错误。 (如果有效,则可能是输入了错误的数据)
下一条消息提供了进一步的信息:
"...The "listener" argument must be of type function. Received type string ('row')"
需要一个 JavaScript 函数来完成工作,但它收到了一个简单的字符串 'row'。
events.js:135
这意味着错误发生在文件 'events.js' 的第 135 行或之前。
TediusJs API Request Docs,提供了参考例子:
request.on('row', function (columns) { /* code to process rows */ });
在您的示例中,我们发现:
Request.on('requestCompleted',"row", function (columns) {
很可能应该是:
Request.on("row", function (columns) {
虽然我不确定你的示例中的哪一行是第 135 行。