Hi everyone c: I've been working in a Node.js project where I bring data from a JSON file and use it with an API to bring weather, and other things, I'm trying to write the new data into another JSON file using require.js but it does not seems to work, here is my code, hope you can help me :)
const fs = require('fs');
var myObject = {
Latitud: data.coord.lat,
Longitud: data.coord.lon,
Temperatura: data.main.temp,
Ciudad: data.name,
Humedad: data.main.humidity,
Descripción: data.weather[0].description,
Viento: data.wind.speed,
};
// convert JSON object to a string
const _myObject = JSON.stringify(myObject);
// write file to disk
fs.writeFile('../js/datareceived.json', _myObject, 'utf8', (err) => {
if (err) {
console.log(`Error writing file: ${err}`);
} else {
console.log(`File is written successfully!`);
}
});
and here is the full script
async function calcWeather(){
const info = await fetch('../js/data.json') // fetch editable c:
.then(function(response) {
return response.json();
});
for (var i in info) {
const lat = info[i][0].latjson;
const long = info[i][0].lonjson;
const fs = require('fs');
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 = {
Latitud: data.coord.lat,
Longitud: data.coord.lon,
Temperatura: data.main.temp,
Ciudad: data.name,
Humedad: data.main.humidity,
Descripción: data.weather[0].description,
Viento: data.wind.speed,
};
// convert JSON object to a string
const _myObject = JSON.stringify(myObject);
// write file to disk
fs.writeFile('../js/datareceived.json', _myObject, 'utf8', (err) => {
if (err) {
console.log(`Error writing file: ${err}`);
} else {
console.log(`File is written successfully!`);
}
});
});
}
};
and here is the way I load in my html
<script src = "./node_modules/requirejs/require.js"></script>