【问题标题】:Why do I get this error while trying to insert data to SQL Server with NodeJS and Tedious?为什么在尝试使用 NodeJS 和 Tedious 将数据插入 SQL Server 时出现此错误?
【发布时间】:2021-12-24 05:42:34
【问题描述】:

在将数据保存到 SQL Server 数据库的 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);
  }
}

【问题讨论】:

    标签: javascript node.js sql-server tedious


    【解决方案1】:

    错误表明 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 行。

    【讨论】:

    • 抱歉这么晚才回答,伙计,我照你说的做了,知道我收到了Couldn't insert data: Error: Requests can only be made in the LoggedIn state, not the Connecting state (node:9024) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined 有什么想法吗?
    【解决方案2】:

    看来request.on(...)方法的参数太多了,即:

    Request.on('requestCompleted',"row", function (columns)
    

    应该是:

    Request.on("row", function (columns)
    

    【讨论】:

    • 对不起,伙计,直到现在我才能回答,因为我的互联网很糟糕,我只是按照你说的做了,我知道我得到了Couldn't insert data: Error: Requests can only be made in the LoggedIn state, not the Connecting state (node:7452) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined,并且正在寻找解决这个问题的方法,我看到我不得不把我已经拥有的:c
    • 关于错误的“未定义提取”部分,这个其他堆栈答案可能会解决该问题:ReferenceError: fetch is not definedtldr; Node 上默认不包含 Fetch,必须包含。这适用于这里吗?
    • 好的,非常感谢!
    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2017-04-21
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多