0

I'm working in a NodeJS project, and I'm expecting to take data from a JSON and then send it into my app2.mjs to a constant variable, to then send them into an array of objects to after that save it into a DB, the issue I'm having is that I send this data this way:

const info = await fetch("./json/data.json").then(function (response) {
    return response.json();
  });

and I get this message error:

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:371:5)
    at onParseError (node:internal/url:552:9)
    at new URL (node:internal/url:628:5)
    at new Request (file:///X:/X/X/X/X/X/X/X/node_modules/node-fetch/src/request.js:51:16)
    at file:///X:/X/X/X/X/X/X/X/node_modules/node-fetch/src/index.js:38:19
    at new Promise (<anonymous>)
    at fetch (file:///X:/X/X/X/X/X/X/node_modules/node-fetch/src/index.js:36:9)
    at calcWeather (file:///X:/X/X/X/X/X/X/X/app2.mjs:39:22)
    at executeStatement (file:///X:/X/X/X/X/X/X/X/app2.mjs:75:3)
    at Connection.<anonymous> (file:///X:/X/X/X/X/X/X/X/app2.mjs:29:5) {
  input: './json/data.json',
  code: 'ERR_INVALID_URL'

I'm using "node-fetch": "^3.1.0", to load the JSON, here is my data.json (juts a few, cause I have like a hundred data)

[
  [
    {
      "latjson": 0,
      "lonjson": 0,
      "IdOficina": "0",
      "NombreOficinaSN": "X",
      "Zona": "X",
      "NombreEstado": "X"
    }
  ],
  [
    {
      "latjson": 1,
      "lonjson": 1,
      "IdOficina": "1",
      "NombreOficinaSN": "Y",
      "Zona": "Y",
      "NombreEstado": "Y"
    }
  ]
]

and here's the part where I do the fetch

async function calcWeather() {
  const info = await fetch("./json/data.json").then(function (response) {
    return response.json();
  });
  console.log(info)
  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);
        }
      });
  }
  console.log(lstValid);
}

I add here below the way my project is structured, any help is welcomed:

enter image description here

Martín JF
  • 152
  • 2
  • 14
  • From node, `"./json/data.json"` is a relative path to your local machine, as such you don't use fetch, as that's for `http/s` requests, use `readFile` instead. – Keith Nov 22 '21 at 17:03
  • `fetch` makes HTTP calls. If you want to read a file, use Node's built-in fs (file system) module. https://nodejs.org/api/fs.html#filehandlereadfileoptions – Jeremy Thille Nov 22 '21 at 17:07

0 Answers0