0

I made a question a minutes ago that I could solve but now I'm having another problem with my code, I have an array of objects lstValid[]and I need each object to be inserted into a table (using a SP) and I though of a way to made it reading documentation but it's not working, maybe it's just a fool mistake but I don't know how to solve it, my mistake is

TypeError: Cannot read properties of undefined (reading 'Id_Oficina')

as you can see, it executes my SP but it says the attribute Id_Oficina is undefined, do you know why is undefined? why is it reading the array but not the attribute?

Here is the function where I create the objects and insert them into the array:

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

and here's the function where I insert into DB:

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

function executeStatement() {
  calcWeather();
  for (var m = 0; m >= lstValid.length; m++) {
    const request = 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);
        }
      }
    );

    request.addParameter("IdOficina", TYPES.SmallInt, lstValid[m].Id_Oficina);
    request.addParameter("Humedad", TYPES.SmallInt, lstValid[m].Humedad);
    request.addParameter("Nubes", TYPES.SmallInt, lstValid[m].Nubes);
    request.addParameter("Sensacion", TYPES.Float, lstValid[m].Sensacion);
    request.addParameter("Temperatura", TYPES.Float, lstValid[m].Temperatura);
    request.addParameter("Descripcion", TYPES.VarChar, lstValid[m].Descripcion);

    request.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); // connection.execSql(RequestB);??
  }
  request.on("requestCompleted", function (rowCount, more) {
    connection.close();
  }); 
}

I also tried sending them this way but doesn't work either:

 request.addParameter("IdOficina", TYPES.SmallInt, lstValid[m].myObject.Id_Oficina);
7
  • Which is the exact line where the error happens? Commented Nov 19, 2021 at 17:58
  • @LajosArpad in the line where I send the attribute of the object in the array, it's request.addParameter("IdOficina", TYPES.SmallInt, lstValid[m].Id_Oficina); Commented Nov 19, 2021 at 18:00
  • What's the value of lstValid[m]? Can you console.log it or debug it? Commented Nov 19, 2021 at 18:14
  • What's the value of lstValid.length when you enter the loop? If lstValid.length is 0 then lstValid[0] is going to return undefined. Commented Nov 19, 2021 at 23:40
  • sorry for answering so late, I didn't have access to my job's pc, @AlwaysLearning @Lajos Arpad the lstValid.length is 0, can you help me to solve it? Commented Nov 22, 2021 at 14:02

1 Answer 1

1

The problem seems to be a bad condition at the line of

for (var m = 0; m >= lstValid.length; m++) {

This loop initializes m with 0 and increments it while it's greater or equal with the number of elements lstValid has. Since Javascript is a 0-indexed language, lstValid.length is always an invalid index. Valid indexes fulfill this formula

0 <= valid index < lstValid.length

Since your condition checks whether the index is invalid and only then iterates the loop, it will error out at the first iteration if lstValid is empty and it will not execute at all when lstValid is not empty.

Fix

for (var m = 0; m < lstValid.length; m++) {

Explanation

Your error came from the fact that lstValid.length was 0, m was 0 and your code attempted to process member fields of the first element of an empty array. Since there was no first element in the speicifc case, it errored out.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for the explanation mate, but I dont really get why it keeps telling me in my console.log the lenght is still 0 as it should be all the objects I have in my array :c
Got it, now I get ReferenceError: fetch is not defined that's why no object is entering the array, right?
@MartínJF you are welcome! We need to step-by-step debug your code and see where it fails to meet your expectations. You have the call of await fetch("../json/data.json") as the very first operation of your calcWeather function. If you add console.log(info) just before the line of for (var i in info) { what do you see?
@MartínJF you are correct!
The console.log(info) before (var i in info) doesn't do nothing :c
|

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.