I'm working in a NodeJS project that reads data.json
, a JSON file with almost a hundred data with coords (lat and lon) and I would like to use those coords to bring up the weather and other stuff, using the Open Weather API
, after that I need to create an array with all those objects to later, add each object to a DB, I'm expecting to bring that coords from the JSON and then create the objects but I get this message error:
file:///X:/X/X/X/X/X/X/node_modules/node-fetch/src/index.js:95
reject(new FetchError(`request to ${request.url} failed, reason: ${error.message}`, 'system', error));
^
FetchError: request to https://api.openweathermap.org/data/2.5/weather?lat=undefined&lon=undefined&appid=XX&units=metric&lang=sp failed, reason: read ECONNRESET
at ClientRequest.<anonymous> (file:///X:/X/X/X/X/X/X/node_modules/node-fetch/src/index.js:95:11)
at ClientRequest.emit (node:events:390:28)
at TLSSocket.socketErrorListener (node:_http_client:447:9)
at TLSSocket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET',
erroredSysCall: 'read'
}
I'm bringing all my JSON because when I console.log them I get them back, but I don't know how to make this works, here's a little example of the data.json
:
[
[{
"latjson": 1,
"lonjson": 1,
"IdOficina": "1"
}],
[{
"latjson": 2,
"lonjson": 2,
"IdOficina": "2"
}]
]
and here is how I'm trying to insert the data from the API to myObject
async function calcWeather() {
fs.readFile('./json/data.json', 'utf8', function (err, info) {
//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);
}
});
}
});
}
When I go to the link of the API that gives me the error I get:
{"cod":"400","message":"wrong latitude"}
EDIT: The issue seems to be that the Latitude isn't being added to the API, because I get all data when I hardcoded the coords
CodePudding user response:
You are not parsing the array properly. Given the comments and the change to the data structure discussed there, this is the data to parse:
[
{
"latjson": 1,
"lonjson": 1,
"IdOficina": "1"
},
{
"latjson": 2,
"lonjson": 2,
"IdOficina": "2"
}
]
Rather than use for...in you should really use for...of it will make it easier and will keep you out of trouble down the road with other coding you do.
Here is what that would look like:
const fakeAPIKey = 12342456478;
const data = [{
"latjson": "33.44",
"lonjson": "-94.04",
"IdOficina": "1"
},
{
"latjson": 2,
"lonjson": 2,
"IdOficina": "2"
}
];
for (let item of data) {
let url = `https://api.openweathermap.org/data/2.5/onecall?lat=${item.latjson}&lon=${item.lonjson}&exclude=hourly,daily&appid=${fakeAPIKey}`;
console.log(`Official Id: ${item.IdOficina}`);
console.log(url);
console.log('-----------');
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
{"cod":"400","message":"wrong latitude"} you are getting bad request response from the API. It appears from the error message that the latitude is wrong