为啥在尝试使用 NodeJS 和 Tedious 将数据插入 SQL Server 时出现此错误?

     2023-03-14     282

关键词:

【中文标题】为啥在尝试使用 NodeJS 和 Tedious 将数据插入 SQL Server 时出现此错误?【英文标题】: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);
  

【问题讨论】:

【参考方案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,必须包含。这适用于这里吗? 好的,非常感谢!

为啥在使用 PostgreSQL local、ngrok 和 NodeJS 时程序会混合所有连接用户的代码?

】为啥在使用PostgreSQLlocal、ngrok和NodeJS时程序会混合所有连接用户的代码?【英文标题】:WhytheprogrammixesthecodeofallusersconnectedwhenworkingwithPostgreSQLlocal,ngrokandNodeJS?为什么在使用PostgreSQLlocal、ngrok和NodeJS时程序会混合所有连接用户的... 查看详情

为啥我应该在 MongoDB 中使用 NodeJS 在会话案例中搜索用户?

】为啥我应该在MongoDB中使用NodeJS在会话案例中搜索用户?【英文标题】:WhyshouldIsearchinMongoDBforuserinsessioncaseusingNodeJS?为什么我应该在MongoDB中使用NodeJS在会话案例中搜索用户?【发布时间】:2021-08-0302:19:36【问题描述】:我正在... 查看详情

为啥我应该在 MongoDB 中使用 NodeJS 在会话案例中搜索用户?

】为啥我应该在MongoDB中使用NodeJS在会话案例中搜索用户?【英文标题】:WhyshouldIsearchinMongoDBforuserinsessioncaseusingNodeJS?为什么我应该在MongoDB中使用NodeJS在会话案例中搜索用户?【发布时间】:2021-08-0302:19:36【问题描述】:我正在... 查看详情

尝试使用 cloudinary 和 nodeJs 在产品中上传图片

】尝试使用cloudinary和nodeJs在产品中上传图片【英文标题】:TrytouploadimageinproductwithcloudinaryandnodeJs【发布时间】:2021-09-0207:38:30【问题描述】:我正在创建电子商务商店的后端,我必须验证照片。我从一个想法开始,但我的老师... 查看详情

为啥nodejs express服务器在我使用post时返回302?

】为啥nodejsexpress服务器在我使用post时返回302?【英文标题】:whynodejsexpressserverreturn302wheniusepost?为什么nodejsexpress服务器在我使用post时返回302?【发布时间】:2019-08-2715:44:22【问题描述】:我正在创建一个注册和登录系统。我在... 查看详情

为啥在 nodejs 中使用 appmetrics profiler 时我的应用程序性能得到提高

】为啥在nodejs中使用appmetricsprofiler时我的应用程序性能得到提高【英文标题】:whymyapplicationperformancegotincreasedwhileusingappmetricsprofilerinnodejs为什么在nodejs中使用appmetricsprofiler时我的应用程序性能得到提高【发布时间】:2018-01-1711:16... 查看详情

为啥我在使用 zip 或直接使用编辑器在 gcloud 中部署 nodejs 函数时遇到错误?

】为啥我在使用zip或直接使用编辑器在gcloud中部署nodejs函数时遇到错误?【英文标题】:WhyamIencounteringanerrorwhendeployinganodejsfunctioningcloudwithazipordirectlywitheditor?为什么我在使用zip或直接使用编辑器在gcloud中部署nodejs函数时遇到错误... 查看详情

为啥 Chrome 不能检查 Docker 容器中的 nodejs 代码?

】为啥Chrome不能检查Docker容器中的nodejs代码?【英文标题】:WhyChromecan\'tinspectnodejscodeinDockercontainer?为什么Chrome不能检查Docker容器中的nodejs代码?【发布时间】:2018-07-2900:52:51【问题描述】:我尝试在Docker容器中启动简单的nodejs... 查看详情

为啥我在尝试获取我过去的贝宝交易时会收到此错误?

】为啥我在尝试获取我过去的贝宝交易时会收到此错误?【英文标题】:WhydoIgetthiserrorwhentryingtogetmypastpaypaltransactions?为什么我在尝试获取我过去的贝宝交易时会收到此错误?【发布时间】:2020-10-1420:14:19【问题描述】:我正在... 查看详情

为啥在成功通过测试用例后,使用 nodeunit 的 nodejs 测试用例一直在 webstorm 中加载?

】为啥在成功通过测试用例后,使用nodeunit的nodejs测试用例一直在webstorm中加载?【英文标题】:Whynodejstestcasewithnodeunitkeeploadinginwebstormaftersuccessfullypassedthetestcases?为什么在成功通过测试用例后,使用nodeunit的nodejs测试用例一直在w... 查看详情

尝试使用 crypto-js 和 nodejs 解密

】尝试使用crypto-js和nodejs解密【英文标题】:attemptingtodecryptusingcrypto-jsandnodejs【发布时间】:2014-08-0700:42:42【问题描述】:我在微控制器和nodejstcp服务器之间来回通信。微控制器形成一个带有传感器数据的json字符串。然后微控... 查看详情

为啥我在尝试使用 HttpListener 时会收到“AccessDenied”? [复制]

】为啥我在尝试使用HttpListener时会收到“AccessDenied”?[复制]【英文标题】:WhydoIget"AccessDenied"whentryingtouseHttpListener?[duplicate]为什么我在尝试使用HttpListener时会收到“AccessDenied”?[复制]【发布时间】:2010-12-3113:25:28【问... 查看详情

为啥nodejs express post请求中的“body”为空?

】为啥nodejsexpresspost请求中的“body”为空?【英文标题】:whyis\'body\'emptyinnodejsexpresspostrequest?为什么nodejsexpresspost请求中的“body”为空?【发布时间】:2021-08-2112:01:00【问题描述】:我正在尝试捕获Postman发送的正文中的原始数... 查看详情

如何在 Tedious 中运行 sql 查询,为 sql in 运算符提供多个值?

】如何在Tedious中运行sql查询,为sqlin运算符提供多个值?【英文标题】:HowtorunasqlqueryinTedioussupplyingmultiplevaluesforthesqlinoperator?【发布时间】:2020-03-0215:10:36【问题描述】:如果我只提供一个参数,下面的代码就可以工作。如果我... 查看详情

使用 Websockets 和 Nodejs 上传文件

...Nodejs【发布时间】:2014-01-0402:45:29【问题描述】:我正在尝试实现一个文件上传器,其中HTML输入文件由WebSocket发送到Nodejs服务器。尝试从HTML的FileReaderAPI读取BLOB和二进制字符串中的文件,并将其发送到Nodejs服务器,以便将其写... 查看详情

无法在 NodeJS 中使用 Mongoose 和 Typescript 扩展基类

...NodeJS【发布时间】:2016-11-1921:52:02【问题描述】:我正在尝试在NodeJS中使用Mongoose4.5.4和it\'stypings和Typescript(显然)同时使用存储库模式。存储库库:exportclassRepositor 查看详情

尝试在 Nodejs 和 MongoDB 中获取当前登录的用户任务

】尝试在Nodejs和MongoDB中获取当前登录的用户任务【英文标题】:TryingtogetcurrentloggedinusertasksinNodejsandMongoDB【发布时间】:2021-01-0606:22:56【问题描述】:我正在开发一个项目,使管理员能够将任务分配给不同的用户,每个用户应该... 查看详情

为啥我的 jQuery .ajax() 路由的 .done() 方法没有在我的 NodeJS、Express 和 Mongoose 项目中触发?

】为啥我的jQuery.ajax()路由的.done()方法没有在我的NodeJS、Express和Mongoose项目中触发?【英文标题】:WhyismyjQuery.ajax()route\'s.done()methodisnotfiringinmyNodeJS,ExpressandMongooseproject?为什么我的jQuery.ajax()路由的.done()方法没有在我的NodeJS、Expres... 查看详情