javascriptnode.jssql-servertedious

Why do I get this error while trying to insert data to SQL Server with NodeJS and Tedious?


Working in a NodeJS that saves data to a SQL Server Database, it must save data from an array of objects but when I run it I get this error, just looked here and documentation but I don't really understand how to fix it, any help is welcomed. This is the error:

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')

And this is my app.js:

Connection:

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();

and here's where I insert data:

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);
  }
}

Solution

  • Seems that there are too many parameters to the request.on(...) method, i.e.:

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

    Should probably be:

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