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

Time:11-12

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

CodePudding user response:

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)

CodePudding user response:

The error says the JavaScript function received arguments that were different than expected:

...ERR_INVALID_ARG_TYPE('listener', 'Function', listener);

If it has never worked, the function was likely mistyped. (If it has worked, it could be bad data coming in)

The next message gives a further information:

"...The "listener" argument must be of type function. Received type string ('row')"

A JavaScript function to do work was expected, but it received a simple string 'row' instead.

events.js:135

This means the error happened in the file 'events.js' on or before line 135.

The TediusJs API Request Docs, provides a reference example:

request.on('row', function (columns) { /* code to process rows */ });

In your example we find:

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

Most likely it should be:

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

Although I am not positive which line in your example is line 135.

  • Related