0

I'm working on a project in NodeJS, this project takes data from a JSON file (I downloaded node-fetch for this) then I put them into an object to then send all the objects into an array and after that save it to a SQLServer DB, the issue that I'm having is that I cannot make it work, I followed what I watched in NodeJS documentation but I get this error when I type node app2.mjs:

node app2.mjs
Successful connection
***:\***\***\***\***\***\***\node_modules\tedious\lib\connection.js:1700
request.callback(error);
^
TypeError: request.callback is not a function
at ***:\***\***\***\***\***\***\node_modules\tedious\lib\connection.js:1700:17
at processTicksAndRejections (node:internal/process/task_queues:78:11)
PS ***:\***\***\***\***\***\project>

This is my package.json

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "module": "CommonJS",
  "type": "commonjs",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "msnodesqlv8": "^2.4.3",
    "mssql": "^7.2.1",
    "node-fetch": "^3.1.0",
    "nodemon": "^2.0.12",
    "tedious": "^14.0.0"
  }
}

and this is my app2.mjs :

import { Connection } from "tedious";
var lstValid = [];

var config = {
  // Connection stuff (This works)
};

var connection = new Connection(config);

connection.on("connect", function (err) {
  if (err) {
    console.log("Failed to connect!!! ", err);
  } else {
    // If no error, then good to go...
    console.log("Successful connection");
    executeStatement();
  }
});

connection.connect();

const api_key = process.env.apikey;

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,
          Humedad: data.main.humidity,
          Nubes: data.clouds.all,
          Sensacion: data.main.feels_like,
          Temperatura: data.main.temp,
          Descripcion: data.weather[0].description,
        };
        // validation and saving data to array
        if (myObject.Temperatura < 99)
          lstValid.push(myObject);
      });
  }
}

import { Request } from "tedious"; 
import { TYPES } from "tedious";   

function executeStatement() {
  calcWeather();
  for (var m = 0; m <= 5 /*lstValid.length*/; m++) {
    const RequestB = new Request(
      "EXEC USP_BI_CSL_insert_reg_RegistroTemperaturaXidOdicina @IdOficina, @Humedad, @Nubes, @Sensacion, @Temperatura, @Descripcion",
      function (err) {
        if (err) {
          console.log("Couldn't insert data: " + err);
        }
      }
    );
    RequestB.addParameter("IdOficina", TYPES.SmallInt, lstValid[m].myObject.Id_Oficina);
    RequestB.addParameter("Humedad", TYPES.SmallInt, lstValid[m].myObject.Humedad);
    RequestB.addParameter("Nubes", TYPES.SmallInt, lstValid[m].myObject.Nubes);
    RequestB.addParameter("Sensacion", TYPES.Float, lstValid[m].myObject.Sensacion);
    RequestB.addParameter("Temperatura", TYPES.Float, lstValid[m].myObject.Temperatura);
    RequestB.addParameter("Descripcion", TYPES.VarChar, lstValid[m].myObject.Descripcion);

    RequestB.on("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);
        }
      });
    });

    connection.execSql(Request);
  }
}

1 Answer 1

1

The issue was, I was sending connection.execSql(Request); as it should have been connection.execSql(RequestB); because that's how I named my constant variable of the new request:

const RequestB = new Request(
    "EXEC USP_BI_CSL_insert_reg_RegistroTemperaturaXidOdicina @IdOficina, @Humedad, @Nubes, @Sensacion, @Temperatura, @Descripcion",
    function (err) {
        if (err) {
            console.log("Couldn't insert data: " + err);
        }
    }
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.